[html-formfu] adding constraints dynamically

Carl Franks fireartist at gmail.com
Tue Aug 18 08:57:43 GMT 2009


2009/8/15 Steve Rippl <rippls at woodlandschools.org>:
> Lastly, is it possible to and/or constraints?  i.e. "value in range 1-5 ||
> value in range 10-20" etc.

Hi Steve,

I'd generally create a new constraint class, so it can be loaded via
config files, as I avoid putting any form generation code in my
Controller classes.
As you're already using code to create your forms, though, you could do this:

$element->constraint( {
    type => 'Callback',
    callback => sub {
        my $value = shift;
        return( ( $value >= 1 && $value <= 5 ) || ( $value >= 10 &&
$value <= 20 ) );
    },
} );

Or, if you want to use the same constraint more than once:

sub in_range {
    my $value = shift;
    return( ( $value >= 1 && $value <= 5 ) || ( $value >= 10 && $value
<= 20 ) );
};

$element->constraint( {
    type => 'Callback',
    callback => \&in_range,
});

Carl



More information about the HTML-FormFu mailing list