[Dbix-class] How to set callback constraint by yaml file ?

Carl Franks fireartist at gmail.com
Tue Aug 28 19:06:02 GMT 2007


On 28/08/07, chylli <lchangying at gmail.com> wrote:
> Hi. I strugglle with this problem for a long time.
> I want to set a callback constraint on a field, for example, check
> whether the username exists in db when user register. The framework is
> Catalyst. Now I use HTML::FormFu directly to create a form and set
> callback by hand.
> Now I want to switch to Catalyst::Controller::HTML::FormFu. But how
> can I set that constraint in the configure yaml file ? Is it possible
> ? the main reason to set it in yml file is that if i use
> sub bar : Local : FormConfig
>  or
> sub baz : Local : FormConfig('my_config')
> the form has been processed before I can set constraint.
>
> thanks for your help

You can create your own constraint as a subclass of HTML::FormFu::Constraint.
Override constrain_value():

sub constrain_value {
    my ( $self, $value, $params ) = @_;
    ...
}

Return true if the value is ok.
If you're using Catalyst::Controller::HTML::FormFu, you can access the
catalyst context via $self->form->stash->{context}, from which you can
access your models.

And then in your yaml config file, add it by it's package name:

    elements:
      - type: Text
        name: foo
        constraints:
          - type: +MyApp::Constraint::UniqueUsername

(If you need more help - it'd maybe be better to move this over to the
html-formfu list)

Cheers,
Carl



More information about the DBIx-Class mailing list