| 1 |
|
| 2 |
The 4.0 release addresses several lingering concerns I've had with the |
| 3 |
interface but didn't now how to address until now. Here's a summary of |
| 4 |
the parts of the interface that now have updated alternatives. The old |
| 5 |
syntax will still work. |
| 6 |
|
| 7 |
* Using 'validator_packages' re-invents module loading and importing. |
| 8 |
'constraint_methods' can be loaded normally: |
| 9 |
use My::Constraints qw(my_constraint); |
| 10 |
|
| 11 |
* Custom constraint_methods don't need to be named "valid_" or "match_". |
| 12 |
|
| 13 |
* Passing parameters to constraints was awkward. It looked like this: |
| 14 |
|
| 15 |
my_field => { |
| 16 |
constraint => 'my_constraint', |
| 17 |
params => [\'string',\'bling'], |
| 18 |
} |
| 19 |
|
| 20 |
constraint_methods allows normal looking parameter passing: |
| 21 |
|
| 22 |
my_field => FV_length_between(10,20); |
| 23 |
|
| 24 |
* Using a 'constraint_method' as a hash was awkward, because you had |
| 25 |
to use a hashref to pass it in: |
| 26 |
|
| 27 |
constraints => |
| 28 |
my_date_field => { |
| 29 |
constraint_method => 'date_and_time' |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
Now these kinds of constraints can be used with less effort with the |
| 34 |
'constraint_methods' profile key: |
| 35 |
|
| 36 |
constraint_methods => { |
| 37 |
my_date_field => 'date_and_time', |
| 38 |
} |
| 39 |
|
| 40 |
All of the built-in constraints have been updated to support this alternate |
| 41 |
interface in which constraint subroutines actually look and work like normal |
| 42 |
subroutines. |
| 43 |
|
| 44 |
Where there is no need to convert old code, here is an example difference of |
| 45 |
using a built-in constraint with the new syntax: |
| 46 |
|
| 47 |
BEFORE: |
| 48 |
|
| 49 |
constraints => { |
| 50 |
my_email => 'email', |
| 51 |
} |
| 52 |
|
| 53 |
AFTER: |
| 54 |
|
| 55 |
constraint_methods => { |
| 56 |
my_email => email(), |
| 57 |
} |
| 58 |
|
| 59 |
As well, the Regexp::Common support has been improved so that you can |
| 60 |
call these routines through the normal subroutine interface provided, |
| 61 |
and DFV will keep track of the name of the constraint for later use |
| 62 |
in the 'msgs' error message system. |
| 63 |
|
| 64 |
BEFORE: |
| 65 |
|
| 66 |
constraints => { |
| 67 |
my_ip => { |
| 68 |
constraint => 'RE_net_IPv4', |
| 69 |
params => [ \'-sep'=> \' ' ], |
| 70 |
}, |
| 71 |
} |
| 72 |
|
| 73 |
AFTER: |
| 74 |
|
| 75 |
constraint_methods => { |
| 76 |
my_ip => FV_net_IPv4Z('-sep'=> ' '); |
| 77 |
} |
| 78 |
|
| 79 |
The 'filter' code has not yet been updated. It has been less of an issue |
| 80 |
since I've never seen a filter that took parameters. If someone wants to work |
| 81 |
on this update, feel free. |
| 82 |
|
| 83 |
* Writing your own constraints is easier. This is where the secret lies for |
| 84 |
making all of these improvements possible. |
| 85 |
|
| 86 |
Here are the fundamental problems that had to be solved: When declaring a |
| 87 |
constraint, you are really declaring a function that needs to be called, but |
| 88 |
you can't actually call the function then, because you don't have the value |
| 89 |
to validate yet, or the DFV object, or the other data which you might want to |
| 90 |
refer to. However, you might have /some/ parameters you want to provide, such |
| 91 |
as minimum and maximum value on a coolness() constraint you've written. |
| 92 |
|
| 93 |
The solution? Closures. This is the concept that took me years to understand |
| 94 |
the value of. The subroutine called in the profile will take the parameters |
| 95 |
that you know at that time. It then returns a customized anonymous subroutine. |
| 96 |
When this is called, it will have everything that's needed to complete job. |
| 97 |
Let's look at an example. |
| 98 |
|
| 99 |
sub coolness { |
| 100 |
my ($min_cool,$max_cool) = @_; |
| 101 |
return sub { |
| 102 |
my $dfv = shift; |
| 103 |
|
| 104 |
# Name it to refer to in the 'msgs' system. |
| 105 |
$dfv->name_this('coolness'); |
| 106 |
|
| 107 |
my $val = $dfv->get_current_constraint_value(); |
| 108 |
|
| 109 |
return ( ($val >= $min_cool) && ($val <= $max_cool) ); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
Getting this far leaves one wrinkle left to work out. How can we refer to values of other |
| 114 |
fields that have been submitted? |
| 115 |
|
| 116 |
This is partly solved by the C<$dfv->get_input_data()> method that lets you get at the hash |
| 117 |
of input data from within the constraint method. |
| 118 |
|
| 119 |
The following is recommended (but not required) for refering to other fields. |
| 120 |
I suggest making the final parameter a hashref that names a 'fields' arrayref. |
| 121 |
Like this: |
| 122 |
|
| 123 |
coolness(1,10,{fields => [qw/personality smarts good_looks/]}); |
| 124 |
|
| 125 |
This would indicate that you are going to use the values of these three |
| 126 |
fields as factors in your C<coolness()> constraint method. |