[Catalyst] HTML::Widget and optional form fields (with checks)

Carl Franks fireartist at gmail.com
Thu Mar 30 10:11:25 CEST 2006


If I understand your question, your code already does this.

The regex constraint always passes if the value is empty or undef.

I added this to the end of your code:
    my $result = $w->process;
    print Data::Dumper::Dumper [$result->valid];

And I got:
    $VAR1 = [
              'birthdate',
              'username'
            ];

Showing that even though nothing was passed to $w->process(),
birthdate was still listed in $result->valid().

If you want to ensure that a value passes a constraint, and is
defined, you must use the 'All' constraint.

Carl


On 29/03/06, Michele Beltrame <mb at italpro.net> wrote:
> Hello!
>
> I have a small problem in defining optional fields with checks (if not
> empty) with HTML::Widget.
>
> I have a form similar to thee one you can see at the end of this
> message. I use strict(1) and the Maybe constraint because I want to cut
> out from the "valid list" some fields which have nothing to do with the
> data of the form itself (I use them for redirecting and such). With the
> Maybe constraint and strict(1) I ensure that only the fields I define
> here, no matter if they're empty or not, go into the "valid list".
>
> However, I have a need which I don't know how to fullfill. I'd like the
> "birthdate" field to be considered valid if it matches the Regex shown
> below OR if it's empty; that is, it doesn't have to be considered valid
> if it's not empty, unless it matches the Regex.
>
> Is there a way to accomplish this? The code I've come up with requires
> the "birthdate" field to match the regex anyway.
>
>     my $w  = $c->widget('emodify');
>     [...]
>     $w->indicator('idrole');
>     $w->strict(1);
>
>     $w->element('Textfield', 'username');
>     $w->element('Textfield', 'birthdate');
>
>     $w->constraint('All', qw/username/);
>     $w->constraint('Maybe', qw/birthdate/);
>     $w->constraint('Regex', qw/birthdate/)->regex(
>         qr/\A \d{4}-\d{2}-\d{2} \z/xms
>     );
>
> Thanks in advance,
> Michele.
>
> --
> Michele Beltrame
> http://www.varlogarthas.net/
> ICQ# 76660101
> Informativa privacy: http://www.italpro.net/em.html
>
> _______________________________________________
> Catalyst mailing list
> Catalyst at lists.rawmode.org
> http://lists.rawmode.org/mailman/listinfo/catalyst
>



More information about the Catalyst mailing list