[html-formfu] force error on field?

Josef Chladek josef.chladek at gmail.com
Tue Sep 25 14:41:56 GMT 2007


Am 25.09.2007 um 15:19 schrieb Carl Franks:

> On 25/09/2007, Josef Chladek <josef.chladek at gmail.com> wrote:
>> hello,
>>
>> I read that one should not use the DBIC::Unique constraint - so I  
>> eval:
>
> Although it won't be in the core until there's a consistent DBIC
> interface, there's nothing to stop you using it for now if you don't
> mind using non-cpan modules.
>
>> eval {$row->populate_from_formfu( $c->stash->{form} );};
>>
>> and then check if $@ has the message "Duplicate entry ... for key
>> 'email'"
>>
>> how would I now set the error for the field 'email' in my form? or
>
> You'll need a constraint especially for that error - you could use a
> Callback, as that always passes if you don't provide a callback()
> routine - so it won't interfere in any way.
>
> e.g.
> ---
> elements:
>   - name: email
>   - constraints:
>     type: Callback
>     message: 'Email already taken'
>
> Then, after your test:
>
> if ( $@ =~ m/duplicate entry for key 'email'/i ) {
>     $form->get_field('email')->get_constraint('Callback')- 
> >force_error(1);
>     $form->process;
>     # then redisplay the form as normal
> }
>
> You'll also need to update to svn version 426, as I've just fixed a
> bug with force_errors().
>
> Carl

thanks, the line above should read:
     $form->get_field('email')->get_constraint('Callback')- 
 >force_errors(1);
(force_errors instead of force_error), otherwise it works now!

thanks again...
josef



More information about the HTML-FormFu mailing list