Skip Menu |

This queue is for tickets about the Data-FormValidator CPAN distribution.

Maintainer(s)' notes

This is the bug queue for Data::FormValidator.

Report information
The Basics
Id: 76842
Status: rejected
Priority: 0/
Queue: Data-FormValidator

People
Owner: Nobody in particular
Requestors: BESSARABV [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in:
  • 4.65
  • 4.70
Fixed in: (no value)



Subject: Empty line is considered to be a correct value
I'm expecting this test to pass: #!/usr/bin/perl use strict; use warnings FATAL => 'all'; use Data::FormValidator; use Test::More tests => 4; my $validate_rules = { optional => [qw( letter letter_or_null )], constraint_methods => { letter => qr/^[A-F]$/, letter_or_null => qr/^(?:[A-F])?$/, }, }; my $tests = [ { block => { letter => 'A', }, status => 1 }, { block => { letter_or_null => 'A', }, status => 1 }, { block => { letter => '', }, status => 0 }, { block => { letter_or_null => '', }, status => 1 }, ]; foreach my $t (@{$tests}) { my $results = Data::FormValidator->check($t->{block}, $validate_rules); my $status; if ( $results->has_invalid or $results->has_missing or $results->has_unknown ) { $status = 0; } else { $status = 1; } my $msg = 'Block is ' . ($t->{status} ? 'valid' : 'invalid'); is ($status, $t->{status}, $msg); } __END__ The output of the test: 1..4 ok 1 - Block is valid ok 2 - Block is valid not ok 3 - Block is invalid # Failed test 'Block is invalid' # at a.pl line 63. # got: '1' # expected: '0' ok 4 - Block is valid # Looks like you failed 1 test of 4.
An empty value is considered "missing". Constraints aren't applied to missing values, so the regex is never considered.
Thank you for the quick response. From my point of view it is incorrect to considered empty values as "missing". Just for example: the 'fax' field in the users data. User can enter some fax number, but later he can remove it. In this case the empty value is the correct value. Hope that some day this will appear in Data::FormValidator. Thank you anyway.
On Fri Apr 27 10:03:37 2012, BESSARABV wrote: Show quoted text
> Thank you for the quick response. > > From my point of view it is incorrect to considered empty values as > "missing". Just for example: the 'fax' field in the users data. User
can Show quoted text
> enter some fax number, but later he can remove it. In this case the > empty value is the correct value. > > Hope that some day this will appear in Data::FormValidator. Thank you > anyway.
Have you found the configuration option called 'missing_optional_valid' ? That sounds like what you want: https://metacpan.org/module/Data::FormValidator#missing_optional_valid I have it set as the default in my own applications. I would have made it the default in Data::FormValidator as well, but long ago when I took over maintenance, I was concerned about breaking apps that depended on the old behavior. Mark
Thank you. I've seen that parameter, but I doesn't solved by problem. Now I have looked to it more thoroughly. I've added missing_optional_valid => 1 to my profile structure. After that fields with empty values have appeared in $results->valid. But values with missing options are not passed to constraint methods. And this is the problem. For some fields I want to allow empty lines, but for others empty lines are incorrect. I would like to check it in constraint methods, so what do you think about adding parameter 'examine_empty_fields_in_methods'?
I appreciate your feedback but I'm sorry, something like that won't be added. ( Unless perhaps you find several other people who also strongly want the feature. ) If you'd like, you could use a filter which converts particular empty strings to another value, which you could then test with a constraint. Mark
Thank you. I understand your position that you are not planning to implement that. But can you please describe why you don't? Do you think that 1) this is incorrect behaviour or 2) you think that is is not needed for the users of this module or 3) you don't have time to add this feature or 4) maybe somethins else?
On Sat Apr 28 13:05:34 2012, BESSARABV wrote: Show quoted text
> Thank you. I understand your position that you are not planning to
implement Show quoted text
> that. But can you please describe why you don't? > > Do you think that 1) this is incorrect behaviour or 2) you think that
is is Show quoted text
> not needed for the users of this module or 3) you don't have time to
add Show quoted text
> this feature or 4) maybe somethins else?
I think the feature would be rarely used and has reasonable alternatives. Meanwhile, it would add it to the complexity of the code and documentation and increase the risk of other bugs. The module has been in use for several years and has hundreds if not thousands of users. Having a single person ask for a feature is not a good indication that it would be generally useful. You are welcome to promote the idea on the mailing list mentioned in the documentation or elsewhere, and see if you can find anyone else who is interested. If a number of people were interested, I would consider changing my mind, but I don't anticipate that happening. Mark
Thank you for the explanation.
To make this ticket 100% complete I'm putting here the fixed script that do what I exect. Many thanks to Mark who has suggested this solution. #!/usr/bin/perl use strict; use warnings FATAL => 'all'; use Data::FormValidator; use Test::More tests => 4; my $null = 'null'; my $validate_rules = { optional => [qw( letter letter_or_null )], filters => [ sub { my $val = $_[0]; $val = $null if $val eq ''; return $val; } ], constraint_methods => { letter => qr/^[A-F]$/, letter_or_null => qr/^[A-F]|$null$/, }, }; my $tests = [ { block => { letter => 'A', }, status => 1 }, { block => { letter_or_null => 'A', }, status => 1 }, { block => { letter => '', }, status => 0 }, { block => { letter_or_null => '', }, status => 1 }, ]; foreach my $t (@{$tests}) { my $results = Data::FormValidator->check($t->{block}, $validate_rules); my $status; if ( $results->has_invalid or $results->has_missing or $results->has_unknown ) { $status = 0; } else { $status = 1; } my $msg = 'Block is ' . ($t->{status} ? 'valid' : 'invalid'); is ($status, $t->{status}, $msg); } __END__ The output of the test: 1..4 ok 1 - Block is valid ok 2 - Block is valid ok 3 - Block is invalid ok 4 - Block is valid
I'm sorry for writing here once again, but I have a bug in my previous script. I'm changin empty string to some value in in filters but I'm not chaiging it back. Here is the fixed version. #!/usr/bin/perl use strict; use warnings FATAL => 'all'; use Data::FormValidator; use Test::More tests => 4; my $null = 'null'; my $validate_rules = { optional => [qw( letter letter_or_null )], filters => [ sub { my $val = $_[0]; $val = $null if $val eq ''; return $val; } ], constraint_methods => { letter => qr/^[A-F]$/, letter_or_null => qr/^[A-F]|$null$/, }, }; my $tests = [ { block => { letter => 'A', }, status => 1 }, { block => { letter_or_null => 'A', }, status => 1 }, { block => { letter => '', }, status => 0 }, { block => { letter_or_null => '', }, status => 1 }, ]; foreach my $t (@{$tests}) { my $results = Data::FormValidator->check($t->{block}, $validate_rules); my $status; if ( $results->has_invalid or $results->has_missing or $results->has_unknown ) { $status = 0; } else { $status = 1; } my $msg = 'Block is ' . ($t->{status} ? 'valid' : 'invalid'); is ($status, $t->{status}, $msg); my $valid_block = $results->valid; foreach my $k ( keys %{$valid_block}) { if ($valid_block->{$k} eq $null) { $valid_block->{$k} = ''; }; }; # now in $valid_block we have the same values as in original block }