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 = shift->SUPER::new(@_);
>     my $validator = FormValidator::Simple->new;
>     
>     $validator->set_messages(
>         $_[0]->path_to('root', 'cart', 'messages.yml')
>     );
> 
>     $self->{'validator'} = $validator;
> 
>     return $self;
> };
> 
> ...
> 
> sub save : Local {
>     my ($self, $c) = @_;
> 
>     if ($c->req->method eq 'POST') {
>         #my $validator = FormValidator::Simple->new;
>         #$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 = $self->{'validator'}->check makes no diff
>         my $results = $self->{'validator'}->results;
> 

>         if ($results->success) {
>             if (my $cart = $c->forward('load')) {
>                 $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'} = $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

Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________
List: [email protected]
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/[email protected]/
Dev site: http://dev.catalyst.perl.org/

Reply via email to