[html-formfu] Unable to create a Callback constraint via populate()

Michele Beltrame mb at cattlegrid.info
Wed Apr 30 15:38:23 BST 2008


Hi Carl!

> Thanks, that's now fixed in svn trunk (r998) by switching on
> Storable's support for deserializing/eval-ing CODE-refs.

Great, thanks!

> Note that any code-ref used cannot make use of anything outside the
> scope of that code-ref.
> For example, the code-ref in the test file
> t/bugs/populate_element_coderef.t needs to call Test::More::ok()
> rather than just ok() - this is because the code is eval'ed outside of
> the original scope/package it originally occured in.

Is there any reason behind this design decision? I'm asking because it
would really be very handy to be able to pass use the CODEref as a
lexical closure and therefore access its context, as I used to do with
HTML::Widget.

That would be much useful for creating (Catalyst-related example) something
like:

---------------------------------------------------------------------
sub populate_form {
    my ( $self, $c ) = @_;

    my $check_dupe = sub {
        my $value = shift;

        my $user = $c->model('Dbs::Users')->find($value);

        return defined $user ? 0 : 1;
    }

    # Example with a constraint, but could also be a validator (which
    # makes more sense for checking duplicate users, maybe)
    $c->populate({
        ......
            constraints => [{
                type        => 'Callback',
                callback    => $check_dupe,
                message     => 'This username already exists',
            }],
        ......
    });
}
---------------------------------------------------------------------

Or is there another simple way to accomplish this that maybe I'm not
thinking of? ;-)

Thanks,
Michele.

-- 
Michele Beltrame
http://www.cattlegrid.info/
ICQ 76660101 - MSN mb at italpro.net



More information about the HTML-FormFu mailing list