Friday, May 18, 2007

More Jifty Niftiness

A newly-discovered (by me, at least) favorite feature of my favorite Perl module: Jifty gives me a nice, easy Ajaxy way to canonicalize (read: make it lowercase) and validate (read: check for uniqueness):
package Testy::Model::Test;
use Jifty::DBI::Schema;
use Testy::Record schema {
column a => type is 'text';
column b => references Testy::Model::User;
};

sub canonicalize_a {
my ($class,$a) = @_;
return lc($a);
}

sub validate_a {
my ($class,$a) = @_;
my $unique = Testy::Model::TestCollection->new();
$unique->limit(column => 'a', operator => '=',
value => $a);
$unique->limit(column => 'b', operator => '=',
value => Jifty->web->current_user->id);
return (0, "You already said $a") if $unique->first;
return $a;
}
1;


Basically, this code automagically converts the input to lowercase (AJAX call), then presents my error message if the person has already typed in that value for a.

Plus, it's all done with as little back-and-forth as I can imagine.

Wow.





<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]