| 1 |
package HTML::FormHandler::Manual::Validation; |
| 2 |
# ABSTRACT: validating fields |
| 3 |
|
| 4 |
__END__ |
| 5 |
|
| 6 |
=pod |
| 7 |
|
| 8 |
=encoding UTF-8 |
| 9 |
|
| 10 |
=head1 NAME |
| 11 |
|
| 12 |
HTML::FormHandler::Manual::Validation - validating fields |
| 13 |
|
| 14 |
=head1 VERSION |
| 15 |
|
| 16 |
version 0.410002 |
| 17 |
|
| 18 |
=head1 SYNOPSIS |
| 19 |
|
| 20 |
L<Manual Index|HTML::FormHandler::Manual> |
| 21 |
|
| 22 |
There are many options for validating fields in FormHandler. Some validation |
| 23 |
is from field attributes, some from form or field methods, some from |
| 24 |
'apply' actions on the fields. |
| 25 |
|
| 26 |
=head1 Field attributes for validation |
| 27 |
|
| 28 |
Each individual field may have additional attributes that relate to validation, |
| 29 |
which are not documented here. See the individual field documentation, linked |
| 30 |
from L<HTML::FormHandler::Manual::Fields>. |
| 31 |
|
| 32 |
=head2 required, required_when |
| 33 |
|
| 34 |
Setting the 'required' flag on a field initiates a check for the existence |
| 35 |
of some value. If the field does not have a value, the 'required' error |
| 36 |
message is issued. |
| 37 |
|
| 38 |
has_field 'section' => ( required => 1, |
| 39 |
messages => { required => 'Please provide a section' } ); |
| 40 |
|
| 41 |
Note that a required flag on a subfield -- a field inside a compound field |
| 42 |
or repeatable field -- does not cause the containing field to be required. |
| 43 |
You need to set 'required' all the way up, if that's the behavior that you |
| 44 |
want. |
| 45 |
|
| 46 |
If a field is empty and *not* required, no other field validation will be |
| 47 |
performed unless the 'validate_when_empty' flag (see below) is set. The form's |
| 48 |
'validate' method, however, will always be called. |
| 49 |
|
| 50 |
There is also the 'required_when' attribute, which works the same way as the |
| 51 |
'when' key on the apply actions. |
| 52 |
|
| 53 |
has_field 'fee' => ( required_when => { 'fie' => 2 } ); |
| 54 |
|
| 55 |
When a 'required' or 'required_when' check fails, a 'missing' flag is set |
| 56 |
in the result: |
| 57 |
|
| 58 |
if ( $field->missing ) { ... } |
| 59 |
|
| 60 |
=head2 range_start, range_end |
| 61 |
|
| 62 |
Starting and ending range for number fields. |
| 63 |
|
| 64 |
=head2 unique |
| 65 |
|
| 66 |
Attribute used by the DBIC model to check for uniqueness. |
| 67 |
|
| 68 |
=head2 validate_when_empty |
| 69 |
|
| 70 |
If its 'validate_when_empty' flag is set to a true value, then a field will |
| 71 |
always undergo validation when its form is processed, even when that field |
| 72 |
is empty. |
| 73 |
|
| 74 |
=head1 Validation methods |
| 75 |
|
| 76 |
=head2 validate_method |
| 77 |
|
| 78 |
You can provide a validation method for a field by setting a coderef with |
| 79 |
'validate_method'. |
| 80 |
|
| 81 |
has_field 'fox' => ( validate_method => \&check_fox ); |
| 82 |
sub check_fox { |
| 83 |
my $self = shift; # self is the fox field |
| 84 |
unless( $self->value eq .... ) { |
| 85 |
$self->add_error('....'); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
=head2 validate_<field_name> |
| 90 |
|
| 91 |
If you provide a 'validate_<field_name>' method it will be automatically used. |
| 92 |
|
| 93 |
has_field 'cat'; |
| 94 |
sub validate_cat { |
| 95 |
my ( $self, $field ) = @_; # self is the form |
| 96 |
unless ( $field->value eq ... ) { |
| 97 |
$field->add_error( '...' ); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
If the field name has periods in it, they should be replaced with underscores. |
| 102 |
|
| 103 |
=head2 form validate method |
| 104 |
|
| 105 |
A form validation method can be used to do cross-validation or validation |
| 106 |
checks that need information from more than one field. |
| 107 |
|
| 108 |
sub validate { |
| 109 |
my $self = shift; |
| 110 |
$self->field('foo')->add_error('....') |
| 111 |
if( $self->field('foo')->value eq '..' && |
| 112 |
$self->field('bar')->value eq '..' ); |
| 113 |
} |
| 114 |
|
| 115 |
=head2 field validate method |
| 116 |
|
| 117 |
You can create a custom field to contain a commonly used validation. The |
| 118 |
validation in a custom field can be done with 'apply' or by using a |
| 119 |
'validate' method. |
| 120 |
|
| 121 |
package MyApp::Form::Field::Custom; |
| 122 |
use HTML::FormHandler::Moose; |
| 123 |
extends 'HTML::FormHandler::Field'; # or a subclass of Field |
| 124 |
|
| 125 |
sub validate { |
| 126 |
.... |
| 127 |
} |
| 128 |
|
| 129 |
=head1 Apply Actions: Filters, transformations, and constraints |
| 130 |
|
| 131 |
The actions in the 'apply' array (stored in the 'actions' attribute) will be |
| 132 |
performed in the order they are specified, allowing fine-grained control over |
| 133 |
inflation and validation. You can check constraints after transformations and |
| 134 |
vice versa. You can weave all three types of actions in any order you need. |
| 135 |
|
| 136 |
The two valid 'apply' array elements are 1) Moose types and 2) hashrefs with one of three |
| 137 |
keys: 'check', 'transform', and 'type'. The hashrefs will usually also have an additional |
| 138 |
key, 'message', with a string, array or coderef providing an error message, |
| 139 |
which is localized. |
| 140 |
|
| 141 |
The 'check' key can point to a regex, arrayref of strings, or coderef. The value of |
| 142 |
the 'transform' key should be a coderef. The value of the 'type' key is a Moose type. |
| 143 |
|
| 144 |
In addition to the check and type keys, you can provide a 'when' key to only |
| 145 |
perform this validation when a particular field is a particular value: |
| 146 |
|
| 147 |
has_field 'fee'; |
| 148 |
has_field 'fie' => ( apply => [ |
| 149 |
{ when => { fee => 1 }, check => qr/when/, message => 'Wrong fie' }, |
| 150 |
]); |
| 151 |
has_field 'fo'; |
| 152 |
has_field 'fum_comp' => ( type => 'Compound' ); |
| 153 |
has_field 'fum_comp.one'; |
| 154 |
has_field 'fum_comp.two' => ( apply => [ |
| 155 |
{ when => { '+fee' => [1,2,3] }, check => qr/when/, message => 'Wrong two' }, |
| 156 |
]); |
| 157 |
|
| 158 |
The field name key in the 'when' hashref is assumed to be a field at the same |
| 159 |
"level" as this field (i.e. a sibling field in a compound). If you want to |
| 160 |
specify a field name from the form, prepend the name with a '+'. |
| 161 |
|
| 162 |
The 'when' hashref can contain multiple key/value pairs. This simply extends its |
| 163 |
test across multiple fields; all fields named in the hashref's keys must match |
| 164 |
their respective values in order for the overall 'when' test to pass. |
| 165 |
|
| 166 |
when => { foo => 3 } # when the foo field value is 3 |
| 167 |
when => { foo => [1,2,3]} # when foo is 1, 2, or 3 |
| 168 |
when => { foo => sub { $_[0] > 0 }} # when foo is greater than 0 |
| 169 |
when => { foo => sub { $_[0] ne ''}} # when foo is the empty string |
| 170 |
|
| 171 |
Transformations and coercions are called in an eval |
| 172 |
to catch the errors. Warnings are trapped in a sigwarn handler. |
| 173 |
|
| 174 |
If the conditions get too complicated to easily fit into a when condition, you |
| 175 |
can always create a validation method instead. |
| 176 |
|
| 177 |
See also L<HTML::FormHandler::Field> and L<HTML::FormHandler::Validate>. |
| 178 |
See L<HTML::FormHandler::Manual::InflationDeflation> for information |
| 179 |
on inflation and deflation. |
| 180 |
|
| 181 |
=head2 Moose types |
| 182 |
|
| 183 |
Moose types can be used to do both constraints and transformations. If a coercion |
| 184 |
exists it will be applied, resulting in a transformation. After coercing, the |
| 185 |
result is checked. You can use type constraints from L<MooseX::Types> |
| 186 |
libraries or defined using L<Moose::Util::TypeConstraints>. |
| 187 |
|
| 188 |
FormHandler supplies a library of Moose types in L<HTML::FormHandler::Types>. |
| 189 |
|
| 190 |
use HTML::FormHandler::Types ('NotAllDigits'); |
| 191 |
has_field 'foo' => ( apply => [ NotAllDigits ] ); |
| 192 |
|
| 193 |
You can create your own library of types, too. Or you can create a type |
| 194 |
constraint in the form: |
| 195 |
|
| 196 |
use Moose::Util::TypeConstraints; |
| 197 |
subtype 'GreaterThan10' |
| 198 |
=> as 'Int' |
| 199 |
=> where { $_ > 10 } |
| 200 |
=> message { "This number ($_) is not greater than 10" }; |
| 201 |
|
| 202 |
has_field 'text_gt' => ( apply=> [ 'GreaterThan10' ] ); |
| 203 |
|
| 204 |
Moose types can also be used for their coercions to do transformations. |
| 205 |
|
| 206 |
subtype 'MyInt' |
| 207 |
=> as 'Int'; |
| 208 |
coerce 'MyInt' |
| 209 |
=> from 'MyStr' |
| 210 |
=> via { return $1 if /(\d+)/ }; |
| 211 |
|
| 212 |
You can also use the 'type' keyword with a Moose type if you want to |
| 213 |
change the message: |
| 214 |
|
| 215 |
has_field 'text_gt' => ( apply => [ |
| 216 |
{ type => 'GreaterThan10', |
| 217 |
message => 'Number is too small' } ] ); |
| 218 |
|
| 219 |
=head2 transform |
| 220 |
|
| 221 |
A 'transform' changes the format of a field's value, and does not |
| 222 |
need a message. It takes a coderef. |
| 223 |
|
| 224 |
has_field 'another_field' => ( |
| 225 |
apply => [ { transform => sub{ sprintf '<%.1g>', $_[0] } } ] |
| 226 |
); |
| 227 |
|
| 228 |
Note that transformed values are not displayed in the HTML form unless |
| 229 |
the 'fif_from_value' flag is set. The transformed values are saved |
| 230 |
to the database or returned in C<< $form->value >>. |
| 231 |
|
| 232 |
=head2 'check' regex |
| 233 |
|
| 234 |
Checks that field value matches the regex. |
| 235 |
|
| 236 |
has_field 'some_field' => ( |
| 237 |
apply => [ { check => qr/aaa/, message => 'Must contain aaa' } ], |
| 238 |
); |
| 239 |
|
| 240 |
You can use regex libraries like L<Regexp::Common> too: |
| 241 |
|
| 242 |
use Regexp::Common ('URI'); |
| 243 |
... |
| 244 |
has_field 'my_url' => ( apply => [ |
| 245 |
{ check => qr/$RE{URI}{HTTP}/, |
| 246 |
message => 'Invalid URL' } ] ); |
| 247 |
|
| 248 |
=head2 'check' arrayref (matches) |
| 249 |
|
| 250 |
Provide an arrayref of strings to match against. |
| 251 |
|
| 252 |
has_field 'set_error' => ( |
| 253 |
apply => [ |
| 254 |
{ check => [ 'abc', 'bbb' ], |
| 255 |
message => 'Must be "aaa" or "bbb"' } |
| 256 |
] |
| 257 |
); |
| 258 |
|
| 259 |
=head2 'check' coderef |
| 260 |
|
| 261 |
Provide a validation function to check. A 'check' coderef will be passed the |
| 262 |
current value of the field and should return true or false. Note that the field |
| 263 |
is passed in as the second argument, to allow simple functions to work properly. |
| 264 |
|
| 265 |
has_field 'callback_pass' => ( |
| 266 |
apply => [ |
| 267 |
{ check => \&check_callback_pass, |
| 268 |
message => 'Must contain number greater than 10', } |
| 269 |
] |
| 270 |
); |
| 271 |
sub check_callback_pass { |
| 272 |
my ( $value, $field ) = @_; |
| 273 |
if( $value =~ /(\d+)/ ) { |
| 274 |
return $1 > 10; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
=head2 message |
| 279 |
|
| 280 |
The message for the above checks can also be an arrayref or coderef. |
| 281 |
The arrayref is useful for localized messages. You can also provide error |
| 282 |
messages for Moose types. |
| 283 |
|
| 284 |
has_field 'message_sub' => ( |
| 285 |
apply => [ |
| 286 |
{ check => [ 'abc' ], |
| 287 |
message => \&err_message } |
| 288 |
] |
| 289 |
); |
| 290 |
sub err_message { |
| 291 |
my ($value, $field ) = @_; |
| 292 |
return $field->name . ': Must be "abc"'; |
| 293 |
} |
| 294 |
has_field 'message_arrayref' => ( |
| 295 |
apply => [ { check => qr/aaa/, |
| 296 |
message => ['Must contain [_1]', 'aaa'] } ], |
| 297 |
); |
| 298 |
has_field 'my_moose_type_field' => ( |
| 299 |
apply => [ { type => SomeType, |
| 300 |
message => 'Invalid ...' } ] ); |
| 301 |
|
| 302 |
=head2 actions in a field class |
| 303 |
|
| 304 |
To declare actions inside a field class use L<HTML::FormHandler::Moose> and |
| 305 |
'apply' sugar: |
| 306 |
|
| 307 |
package MyApp::Field::Test; |
| 308 |
use HTML::FormHandler::Moose; |
| 309 |
extends 'HTML::FormHandler::Field; |
| 310 |
|
| 311 |
apply [ 'SomeConstraint', { check => ..., message => .... } ]; |
| 312 |
|
| 313 |
1; |
| 314 |
|
| 315 |
Actions specified with apply are cumulative. Actions may be specified in |
| 316 |
field classes and additional actions added in the 'has_field' declaration. |
| 317 |
|
| 318 |
You can see examples of field classes with 'apply' actions in the source for |
| 319 |
L<HTML::FormHandler::Field::Money> and L<HTML::FormHandler::Field::Email>, and |
| 320 |
in t/constraints.t. |
| 321 |
|
| 322 |
=head1 Dependency |
| 323 |
|
| 324 |
The 'dependency' attribute is an array of arrays of field names. |
| 325 |
During validation, if any field in a given group has a value that |
| 326 |
matches the pattern /\S/ (non-blank), the 'required' flag |
| 327 |
is set for all of the fields in the group. |
| 328 |
|
| 329 |
has '+dependency' => ( default => sub { |
| 330 |
[ |
| 331 |
['address', 'city', 'state', 'zip'], |
| 332 |
['cc_no', 'cc_expires'], |
| 333 |
], |
| 334 |
}, |
| 335 |
); |
| 336 |
|
| 337 |
You can also use the 'required_when' flag to do something similar. |
| 338 |
|
| 339 |
=head1 AUTHOR |
| 340 |
|
| 341 |
FormHandler Contributors - see HTML::FormHandler |
| 342 |
|
| 343 |
=head1 COPYRIGHT AND LICENSE |
| 344 |
|
| 345 |
This software is copyright (c) 2017 by Gerda Shank. |
| 346 |
|
| 347 |
This is free software; you can redistribute it and/or modify it under |
| 348 |
the same terms as the Perl 5 programming language system itself. |
| 349 |
|
| 350 |
=cut |