[html-formfu] Can't programatically add constraint or validator
callbacks (unless I call $form->process)
Dan Dascalescu
ddascalescu+formfu at gmail.com
Thu Mar 5 10:52:23 GMT 2009
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.
HTH,
Dan
More information about the HTML-FormFu
mailing list