[Catalyst] FormValidator::Simple Sillyness

Christopher H. Laco claco at chrislaco.com
Tue Aug 29 04:55:31 CEST 2006


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

-------------- 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/20060828/8fe8ebae/attachment.pgp 


More information about the Catalyst mailing list