[html-formfu] Add a name property for Callback constraints? Forcing errors

Carl Franks fireartist at gmail.com
Sun Jan 31 20:44:02 GMT 2010


On 28 January 2010 16:37, Oskari 'Okko' Ojala <okko+formfu at frantic.com> wrote:
> Consider this:
>
> - type: Checkbox
>  name: email_permission
>  constraints:
>    - type: Callback
>      message: 'You cannot turn this off when you have pending orders.'
>      name: orders_blocking
>    - type: Callback
>      message: 'You cannot turn this off when you have pending questions.'
>      name: questions_blocking
>
>
> I'd like to reference one of multiple callbacks by name to be able to define
> the error messages in YAML and to define the complicated database checks in
> code. The name would be needed to match the correct Callback, like
>
>    $form
>    ->get_constraint({
>        name => 'orders_blocking',
>        type => 'Callback',
>    })
>    ->force_errors(1);
>
>
> but at the moment name is not allowed for Callbacks:
> "cannot use name() as a setter at ..../_Field.pm"
>
> Is there some other method to match a correct Callback if there are many?

Constraints already have a name() method - it just returns:
    $constraint->field->name()

You could achieve this with:

package HTML::FormFu::Constraint::My::Callback;
use base 'HTML::FormFu::Constraint::Callback';
__PACKAGE__->mk_item_accessors( 'my_name' );
1;

Which you'd use like:
    constraints:
      - type: 'My::Callback'
        message: 'You cannot turn this off when you have pending orders.'
        my_name: orders_blocking

Carl



More information about the HTML-FormFu mailing list