[Catalyst] Catalyst::Plugin::FormValidator questions

Jules Bean jules at jellybean.co.uk
Fri Nov 18 10:31:38 CET 2005


Marcello wrote:
> John Wang ha scritto:
>> Thanks Drew,
>>
>> Putting the use declaration in the controller solved the built-in 
>> constraint method issue.
>>
>> I did some more experimentation with getting a $c context in a custom 
>> constraint method with D::FV and C::P::FV. It seems that constraint 
>> methods in the profile can't take variables as parameters and also 
>> can't take a Catalyst $c->forward as a constraint method. I tried to 
>> pass in the Catalyst context object as a parameter but no dice.
>>
>> It seems like all the variables available to the custom constraint 
>> method come from the $dfv object in the "customized DFV method 
>> subroutine" so the Catalyst context object would have to come from 
>> there as well but I'm guessing this isn't supported. Something like:
>>
>> sub my_custom_constraint {
>>   return sub {
>>     my $dfv = shift;
>>     my $val = $dfv->get_current_constraint_value;
>>     my $data = $dfv->get_input_data;
>>     ...
>>     my $c = $dfv->get_context_obj;
>>     ...
>>   }
>> }
>>
>> Is there a way to access the Catalyst $c in a custom constraint method? 
>
> Catalist::Plugin::Singleton ?

No, no, no, no!

A thousand times no!

Catalyst::Plugin::Singleton is evil! Global variables are evil! They 
will raid your kitchen and eat all your cookies!

sub my_custom_constraint {
    my $c = shift;
  return sub {
    my $dfv = shift;
    my $val = $dfv->get_current_constraint_value;
    my $data = $dfv->get_input_data;
    ...
    ...
  }
}


should work fine. The anonymous sub should have access to the $c which 
is in the enclosing lexical scope. If I understand DFV correctly (and 
I've only glanced at the manual pages) you'd use that like this:

fieldname => my_custom_constrain($c)

HTH,

Jules



More information about the Catalyst mailing list