[html-formfu] Patch to process when Constraint condition in any case

Carl Franks fireartist at gmail.com
Fri Jun 27 11:58:44 BST 2008


2008/6/9 Michele Beltrame <mb at cattlegrid.info>:
> Hello!
>
> I found myself having a Checkbox and an Input field and wanting to check
> requiredness on the input field only if the Checkbox was selected.
> Checkbox is defined as follows:
>
> {
>    name    => 'mycheckbox',
>    type    => 'Checkbox',
>    value   => '1',
> }
>
> The Input field has this constraint:
>
> {
>    type    => 'Required',
>    when    => {
>        field   => 'samehash',
>        value   => '1',
>        not     => 1,
>    },
> }
>
> This doesn't really work when the checbox is not submitted, as its value is
> not defined and the when condition returns true. Since what I needed was to
> check anyway, as I actually wanted the constraint NOT to be applied when the
> checkbox was not selected, I had to patch the Constraint.pm code as per
> attachment so when accepts now a:
>
>        always  => 1
>
> parameter which goes on with the check even if the value of the field to be
> checked (the checkbox in my case) is unsubmitted (=>undef).
>
> Let me know if this patch could be interesting to apply and I'll do (with doc
> and test of course). Also, if you have a better name than "always"... ;-)
>
> If what I'm doing is easy to achiever without the patch and I'm messing
> something obvious, please tell me as well. :-D

I've looked at this a few times, trying to figure out the best way to
deal with this.
I'm not 100% certain, but I think the best solution is to make the
checkbox default to an empty value "", if no value was submitted for
it - but not as a default behaviour, of course.

I've added a 'default_empty_value' method to all fields.
It defaults to false, but if you set it to true, then if the form's
submitted, and the field has no value, it'll get an empty value.
This means you don't have to set any extra options in your 'when' config.

I've added tests, which I think exercise the behaviour you wish. See:
    /trunk/HTML-FormFu/t/constraints/constraint_when_default_empty_value.t
    /trunk/HTML-FormFu/t/constraints/constraint_when_default_empty_value.yml

Do you envisage any problems with this approach?

Carl



More information about the HTML-FormFu mailing list