Re: [dfv] Multiple constraint_methods for a field
Status: Inactive
Brought to you by:
markjugg
|
From: David W. <dav...@va...> - 2016-08-07 19:01:45
|
Thanks
I ended up using FV_and from
Data::FormValidator::Constraints::MethodsFactory to define constraint
methods on my email field as follows.
email => [
{
constraint_method => email ,
name => 'email_valid'
} ,
{
constraint_method =>
FV_and ( email , representative_known (
$self -> dbh ) ) ,
fields => [ qw / society / ] ,
name => 'representative_known'
} ,
] ,
The email method executes twice but the representative_known method only
executes if the email method confirms that the email provided has a
valid format. I didn't want the representative_known method to execute
if the email is invalid as that would be a wasteful database access.
Dave
On 11/07/16 08:57, Jonas B. Nielsen wrote:
> Hi Dave.
>
> Not the prettiest solution. but you could do a two phase validation:
>
> my $dfv = Data::FormValidator->new({
> profile_1 => { # usual profile definition here },
> profile_2 => { # another profile definition },
> });
>
> my $results1 = $dfv->check(\%input_hash,'profile_1’);
>
> my $results2 = $dfv->check(\%input_hash,'profile_2');
>
> Examples lifted from the documentation:
>
> https://metacpan.org/pod/Data::FormValidator
>
> jonasbn
>
>> On 11 Jul 2016, at 01:44, David Williamson <dav...@va...> wrote:
>>
>> I have a form field "email". I want to apply two constraints to it.
>> Firstly, I want to check that the email entered looks valid. Secondly,
>> if (and only if) the email entered looks valid I want to check that is
>> known to be associated with a society. That second check is specific to
>> the particular functionality of my web site.
>>
>> Here is my entry for the email field in constraint_methods.
>>
>> email => [
>> {
>> constraint_method => email ,
>> name => 'email_valid'
>> } ,
>> {
>> constraint_method => representative_known ,
>> fields => [ qw / society / ]
>> } ,
>> ] ,
>>
>> I've used the email constraint from Data::FormValidator::Constraints and
>> provided my own name to associate to my own error message. I have
>> another error message associated with the representative_known constraint.
>>
>> When I test with an invalid email address I get both error messages
>> returned in an array, which suggests to me that both constraints have
>> been executed. How do I stop the representative_known constraint from
>> being run if the email fails the email constraint?
>>
>> Dave
>>
>>
>> ------------------------------------------------------------------------------
>> Attend Shape: An AT&T Tech Expo July 15-16. Meet us at AT&T Park in San
>> Francisco, CA to explore cutting-edge tech and listen to tech luminaries
>> present their vision of the future. This family event has something for
>> everyone, including kids. Get more information and register today.
>> http://sdm.link/attshape
>> _______________________________________________
>> Cascade-DataForm mailing list
>> Cas...@li...
>> https://lists.sourceforge.net/lists/listinfo/cascade-dataform
|