[Catalyst] FormValidator::Simple Sillyness

Christopher H. Laco claco at chrislaco.com
Tue Aug 29 21:06:08 CEST 2006


Christopher H. Laco wrote:
> Somedays I swear I shouldn't be allowed to touch a damn computer.
> I'm working on rewriting the cat helper code in Handel. IT was using
> Data::FormValidator, but I'm now using FormValidator::Simple instead.
> 
> For reasons I don't understand, I can't get the messages to display the
> right thing when I'm using a global instance of a validator, but I have
> no issues if I create on per request:
> 
> In my controller:
> 
>> sub COMPONENT {
>>     my $self >>     my $validator >>     
>>     $validator->set_messages(
>>         $_[0]->path_to('root', 'cart', 'messages.yml')
>>     );
>>
>>     $self->{'validator'} >>
>>     return $self;
>> };
>>
>> ...
>>
>> sub save : Local {
>>     my ($self, $c) >>
>>     if ($c->req->method eq 'POST') {
>>         #my $validator >>         #$validator->set_messages($c->path_to('root', 'cart', 'messages.yml'));
>>
>>         $self->{'validator'}->check($c->req, [
>>             name => [ ['NOT_BLANK'], ['LENGTH', 1, 5] ]
>>         ]);
>>         # this is how the plugin does it
>>         # my $results >>         my $results >>
> 
>>         if ($results->success) {
>>             if (my $cart >>                 $cart->name($c->req->param('name') || 'My Cart');
>>                 $cart->save;
>>                 $c->res->redirect($c->uri_for('/cart/list/'));
>>             };
>>         } else {
>>             foreach my $message (@{$results->messages('save')}) {
>>                 warn $message, "\n";
>>             };
>>
>>             $c->stash->{'results'} >>             $c->forward('default');
>>         };
>>     };
>> };
> 
> My messages.yml file:
> 
>> ---
>> save:
>>   name:
>>     NOT_BLANK: The name field cannot be empty.
>>     LENGTH: The name field must be between 1 and 5 characters.
>>
>>
>>
> 
> 
> If I submit the form with the code as-is, and the name field blank, I
> get 'The name field cannot be empty'. Good. Now, if I submit the form
> with 10 characters in the name field, I still get 'The name field cannot
> be empty', even though a) it's not, and b), $c->req->param('name')
> indeed, isn't empty.
> 
> Now, if I comment out the COMPONENT part, and uncomment the code in
> save() to create an instance of FV::S for each request, it works like a
> champ. I then get 'The name field cannot be empty' when the name field
> is empty, and 'The name field must be between 1 and 5 characters' when
> the field is 6+ characters.
> 
> What the hell am I doing wrong here?
> 
> I've tried FV::S 0.15 and 0.16, and I'm not using the
> Catalyst::Plugin::FormValidator::Simple plugin because I don't believe
> in playing the $c->form wars if someone is already using the
> Data::FormValidator plugin.
> 
> -=Chris

For the sake of this argument, the following change fixes my problem:
in COMPONENT:

> FormValidator::Simple->set_messages(
>    $_[0]->path_to('root', 'cart', 'messages.yml')
> );

instead of:

> $validator->set_messages(
>    $_[0]->path_to('root', 'cart', 'messages.yml')
> );

So, imho, FV::S is not very object vs. class friendly. If I needed to
set two different sets of messages for two seperate parts of an app that
just happen to have the same action name (like save, load, delete, add,
or anything else that is just descript), I'm screwed.

This is even worse if you're running one process (Cat app) that hosts
multiple sites within it, and each site might have it's own set of
messages for form validation (aka, Mango).

-=Chris





-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: OpenPGP digital signature
Url : http://lists.rawmode.org/pipermail/catalyst/attachments/20060829/72c3a9a9/attachment.pgp 


More information about the Catalyst mailing list