[html-formfu] Can't programatically add constraint or validator callbacks (unless I call $form->process)

Carl Franks fireartist at gmail.com
Thu Mar 5 11:17:32 GMT 2009


2009/3/5 Dan Dascalescu <ddascalescu+formfu at gmail.com>:
> As a new FormFu user, I saw that I could modify the form before it got rendered:
>
> sub index :Path :Args(0) :FormConfig {
>    my ( $self, $c ) = @_;
>    my $form = $c->stash->{form};
>
>    my $field = $form->get_all_element({name => 'foo'});
>    $field->label('new label');  # this works
>
>    if ( $form->submitted_and_valid ) {
>    ... the usual
>    }
> }
>
> However, the same is not true of constraints. Adding the code below to
> the index sub, before if ( $form->submitted_and_valid ), has no
> effect:
>
>    $field->constraint({
>        type => 'Callback',
>        callback => \&never_gets_called,
>    });
>
>    $field->validator('Callback')->callback( \&never_gets_called );
>
> I believe another user encountered the same problem:
> http://www.mail-archive.com/html-formfu@lists.scsys.co.uk/msg00970.html
>
> There are two workarounds:
> 1. Add the constraint in the form config file
> 2. Call $form->process before $form->submitted_and_valid. This is
> really counter-intuitive to new users: since $field->label works
> without $form->process, why would $field->validator need a
> $form->process?
>
> If form->process() really needs to be called for callbacks to get
> registered, then maybe the doc for process() should mention this.

The reason your callback never got called, is because all constraints,
validators, inflators, etc. actually get run during process().
submitted_and_valid() only checks the results that were last
calculated during process().

process() should be called after *any* change to the form.
You can get away with it in certain places, such as calling label() on
most fields, but generally, just remember to always call process().

This is the kind of thing that probably can't be documented enough.
It was already mentioned in a couple of places, including
Catalyst-Controller-HTML-FormFu,
but I've now added a fuller explanation in both the HTML-FormFu
synopsis, and the docs for process().

Cheers,
Carl



More information about the HTML-FormFu mailing list