lib/HTML/FormFu/Constraint.pm

filter: Statementstmt 93.4% 157 / 168 Branchbran 70.5% 72 / 102 Conditioncond 78.9% 15 / 19 MC/DCmcdc 66.6% 8 / 12 Subroutinesub 92.5% 25 / 27 Podpod 11.1% 1 / 9 totaltotal 82.4% 278 / 337 SCAR 43.8
CC
64
Cov
84%
_process_when
26.5
process
10.1
repeatable_repeat
9.9
CRAP 79.5
« lib/HTML/FormFu/Role/Element/SingleValueField.pm ↑ Summary lib/HTML/FormFu/Role/Element/Group.pm »
1 404 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package HTML::FormFu::Constraint;
4 404 $HTML::FormFu::Constraint::VERSION = '2.06';
5 # ABSTRACT: Constrain User Input
6  
7 404 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 404 ▶use MooseX::Attribute::Chained;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 404 extends 'HTML::FormFu::Processor';
10  
11 404 ▶use HTML::FormFu::Exception::Constraint;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 404 use HTML::FormFu::Util qw(
13 DEBUG_CONSTRAINTS
14 DEBUG_CONSTRAINTS_WHEN
15 debug
16 404 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 404 ▶use Clone ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 404 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 404 ▶use List::Util 1.33 qw( any all first );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 404 ▶use Scalar::Util qw( reftype blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 404 has not => ( is => 'rw', traits => ['Chained'] );
23 404 has force_errors => ( is => 'rw', traits => ['Chained'] );
24 404 has when => ( is => 'rw', traits => ['Chained'] );
25 404 has only_on_reps => ( is => 'rw', traits => ['Chained'] );
26  
27 sub repeatable_repeat {
28 49 ▶ my ( $self, $repeatable, $new_block ) = @_;
Subroutine repeatable_repeat: called CC=7 SCAR=23.0
Pod undocumented
29  
30 49 my $block_fields = $new_block->get_fields;
31  
32 # rename any 'when' fields
33 {
34 49 my $when = $self->when;
35  
36 49 ▶ if ( my $name = $when->{field} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $name = $when->{'field'}) { } 1 48 49
elsif (my $names = $when->{'fields'}) { } 0 48 48
37 1 my $field = $repeatable->get_field_with_original_name( $name,
38 $block_fields );
39  
40 1 ▶ if ( defined $field ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field) 1 0 1
41 1 ▶ DEBUG_CONSTRAINTS && debug(
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS() and debug(sprintf("Repeatable renaming constraint 'when{field}' '%s' to '%s'", $name, $field->nested_name)) 0 1 1
42 sprintf
43 "Repeatable renaming constraint 'when{field}' '%s' to '%s'",
44 $name, $field->nested_name,
45 );
46  
47 1 $when->{field} = $field->nested_name;
48 }
49 }
50 elsif ( my $names = $when->{fields} ) {
51 0 for my $name (@$names) {
52 0 my $field = $repeatable->get_field_with_original_name( $name,
53 $block_fields );
54  
55 0 ▶ if ( defined $field ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field) 0 0 0
56 0 $when->{field} = $field->nested_name;
57 }
58 }
59 }
60 }
61 }
62  
63 ▶sub pre_process { }
Subroutine pre_process: called CC=1 SCAR=0.0
Pod undocumented
64  
65 sub process {
66 405 ▶ my ( $self, $params ) = @_;
Subroutine process: called CC=10 SCAR=23.1
Pod undocumented
67  
68 405 ▶ return unless $self->_run_this_rep;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_run_this_rep 15 390 405
69  
70 390 my $value = $self->_find_field_value($params);
71  
72 390 my @errors;
73  
74 # check when condition
75 390 ▶ if ( !$self->_process_when($params) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_process_when($params)) 29 361 390
76 29 ▶ DEBUG_CONSTRAINTS && debug('fail when() check - skipping constraint');
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS() and debug("fail when() check - skipping constraint") 0 29 29
77 29 return;
78 }
79  
80 361 ▶ if ( ref $value eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 14 347 361
81 14 push @errors, eval { $self->constrain_values( $value, $params ) };
82  
83 14 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 13 14
84 1 push @errors,
85 $self->mk_errors(
86 { pass => 0,
87 message => $@,
88 } );
89 }
90 }
91 else {
92 347 my $ok = eval { $self->constrain_value( $value, $params ) };
93  
94 347 ▶ DEBUG_CONSTRAINTS && debug( 'CONSTRAINT RETURN VALUE' => $ok );
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS() and debug("CONSTRAINT RETURN VALUE", $ok) 0 347 347
95 347 ▶ DEBUG_CONSTRAINTS && debug( '$@' => $@ );
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS() and debug("\$\@", $@) 0 347 347
96  
97 347 ▶ push @errors,
Branch: 1 branch
BranchTrueFalseTotal
$@ || !$ok ? : 111 236 347
Condition100%3/3
$@ or CORE::not $ok
l!l&&r!l&&!r
3108236
MC/DC100%2/2
$@ or CORE::not $ok
$@ CORE::not $ok
Truth table100%3/3
A or not(B)
A= $@B= CORE::not $ok
ABresult
1X1
011
000
98 $self->mk_errors(
99 { pass => ( $@ || !$ok ) ? 0 : 1,
100 message => $@,
101 } );
102 }
103  
104 361 return @errors;
105 }
106  
107 sub _run_this_rep {
108 439 ▶ my ($self) = @_;
Subroutine _run_this_rep: called CC=5 SCAR=16.1
109  
110 439 ▶ my $only_on_reps = $self->only_on_reps
Branch: 1 branch
BranchTrueFalseTotal
my $only_on_reps = $self->only_on_reps or return 1 378 61 439
111 or return 1;
112  
113 61 ▶ my $current_rep = $self->field->repeatable_count
Branch: 1 branch
BranchTrueFalseTotal
my $current_rep = $self->field->repeatable_count or return 1 0 61 61
114 or return 1;
115  
116 61 ▶ $only_on_reps = [$only_on_reps]
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($only_on_reps) || "") ne "ARRAY" 18 43 61
Condition100%2/2
builtin::reftype($only_on_reps) || ''
l!l
4318
MC/DC100%1/1
builtin::reftype($only_on_reps) || ''
builtin::reftype($only_on_reps)
Truth table100%2/2
A
A= builtin::reftype($only_on_reps)
Aresult
11
00
117 if ( reftype($only_on_reps) || '' ) ne 'ARRAY';
118  
119 61 ▶ return first { $current_rep == $_ } @$only_on_reps;
Subroutine __ANON__: called CC=1 SCAR=0.0
120 }
121  
122 sub _find_field_value {
123 390 ▶ my ( $self, $params ) = @_;
Subroutine _find_field_value: called CC=8 SCAR=21.8
124  
125 390 my $value = $self->get_nested_hash_value( $params, $self->nested_name );
126  
127 my @fields_with_this_name
128 390 = @{ $self->form->get_fields( { nested_name => $self->nested_name } ) };
129  
130 390 ▶ if ( @fields_with_this_name > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields_with_this_name > 1) 10 380 390
131 10 my $field = $self->parent;
132 10 my $index;
133  
134 10 ▶ for ( my $i = 0; $i <= $#fields_with_this_name; ++$i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#fields_with_this_name) 15 0 15
135 15 ▶ if ( $fields_with_this_name[$i] eq $field ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields_with_this_name[$i] eq $field) 10 5 15
136 10 $index = $i;
137 10 last;
138 }
139 }
140  
141 10 ▶ croak 'did not find ourself - how can this happen?'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 0 10 10
142 if !defined $index;
143  
144 10 ▶ if ( ( reftype($value) || '' ) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ((builtin::reftype($value) || '') eq 'ARRAY') { } 10 0 10
elsif ($index == 0) { } 0 0 0
Condition50%1/2
builtin::reftype($value) || ''
l!l
100
MC/DC0%0/1
builtin::reftype($value) || ''
builtin::reftype($value)
Truth table50%1/2
A
A= builtin::reftype($value)
Aresult
11
00
145 10 $value = $value->[$index];
146 }
147 elsif ( $index == 0 ) {
148  
149 # keep $value
150 }
151 else {
152 0 undef $value;
153 }
154 }
155  
156 390 return $value;
157 }
158  
159 sub constrain_values {
160 23 ▶ my ( $self, $values, $params ) = @_;
Subroutine constrain_values: called CC=6 SCAR=18.2
Pod undocumented
161  
162 23 my @errors;
163  
164 23 for my $value (@$values) {
165 45 my $ok = eval { $self->constrain_value( $value, $params ) };
166  
167 45 ▶ DEBUG_CONSTRAINTS && debug( 'CONSTRAINT RETURN VALUE' => $ok );
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS() and debug("CONSTRAINT RETURN VALUE", $ok) 0 45 45
168 45 ▶ DEBUG_CONSTRAINTS && debug( '$@' => $@ );
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS() and debug("\$\@", $@) 0 45 45
169  
170 45 ▶ push @errors,
Branch: 1 branch
BranchTrueFalseTotal
$@ || !$ok ? : 1 44 45
Condition66%2/3
$@ or CORE::not $ok
l!l&&r!l&&!r
0144
MC/DC50%1/2
$@ or CORE::not $ok
$@ CORE::not $ok
Truth table66%2/3
A or not(B)
A= $@B= CORE::not $ok
ABresult
1X1
011
000
171 $self->mk_errors(
172 { pass => ( $@ || !$ok ) ? 0 : 1,
173 message => $@,
174 } );
175 }
176  
177 23 return @errors;
178 }
179  
180 sub constrain_value {
181 0 ▶ croak "constrain_value() should be overridden";
Subroutine constrain_value: not called CC=1 SCAR=6.9
Pod undocumented
182 }
183  
184 sub mk_errors {
185 400 ▶ my ( $self, $args ) = @_;
Subroutine mk_errors: called CC=5 SCAR=16.1
Pod undocumented
186  
187 400 my $pass = $args->{pass};
188 400 my $message = $args->{message};
189  
190 400 my @errors;
191 400 ▶ my $force = $self->force_errors || $self->parent->force_errors;
Condition100%3/3
$self->force_errors || $self->parent->force_errors
l!l&&r!l&&!r
84388
MC/DC100%2/2
$self->force_errors || $self->parent->force_errors
$self->force_errors $self->parent->force_errors
Truth table100%3/3
A || B
A= $self->force_errorsB= $self->parent->force_errors
ABresult
1X1
011
000
192  
193 400 ▶ if ( !$pass || $force ) {
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::not $pass or $force) 126 274 400
Condition100%3/3
CORE::not $pass or $force
l!l&&r!l&&!r
1188274
MC/DC100%2/2
CORE::not $pass or $force
CORE::not $pass $force
Truth table100%3/3
not(A) or B
A= CORE::not $passB= $force
ABresult
1X1
011
000
194 126 my $error = $self->mk_error($message);
195  
196 126 ▶ $error->forced(1) if $pass;
Branch: 1 branch
BranchTrueFalseTotal
if $pass 8 118 126
197  
198 126 push @errors, $error;
199 }
200  
201 400 return @errors;
202 }
203  
204 sub mk_error {
205 194 ▶ my ( $self, $err ) = @_;
Subroutine mk_error: called CC=3 SCAR=12.0
Pod undocumented
206  
207 194 ▶ if ( !blessed $err || !$err->isa('HTML::FormFu::Exception::Constraint') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($err) and $err->isa("HTML::FormFu::Exception::Constraint")) 194 0 194
Condition33%1/3
builtin::blessed($err) and $err->isa("HTML::FormFu::Exception::Constraint")
!ll&&!rl&&r
19400
MC/DC0%0/2
builtin::blessed($err) and $err->isa("HTML::FormFu::Exception::Constraint")
builtin::blessed($err) $err->isa("HTML::FormFu::Exception::Constraint")
Truth table33%1/3
A and B
A= builtin::blessed($err)B= $err->isa("HTML::FormFu::Exception::Constraint")
ABresult
0X0
100
111
208 194 $err = HTML::FormFu::Exception::Constraint->new;
209 }
210  
211 194 return $err;
212 }
213  
214 sub _process_when {
215 501 ▶ my ( $self, $params ) = @_;
Subroutine _process_when: called CC=24 SCAR=32.8
216  
217 # returns 1 if when condition is fulfilled or not defined
218 # returns 0 if when condition is defined and not fulfilled
219 # If it's a callback, return callback's return value (so 'when'
220 # condition is met if callback returns a true value)
221  
222 # get when condition
223 501 my $when = $self->when;
224 501 ▶ return 1 if !defined $when;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $when 425 76 501
225  
226 # check type of 'when'
227 76 ▶ croak "Parameter 'when' is not a hash ref" if ref $when ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $when ne "HASH" 0 76 76
228  
229 # field or callback must be defined
230 76 my $when_field = $when->{field};
231 76 my $when_fields = $when->{fields};
232 76 my $when_any_field = $when->{any_field};
233 76 my $when_callback = $when->{callback};
234  
235 croak
236 "'field', 'fields', 'any_field' or 'callback' key must be defined in 'when'"
237 76 ▶ if all { !defined } $when_field, $when_fields, $when_any_field,
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if &all(sub { CORE::not defined $_; } , $when_field, $when_fields, $when_any_field, $when_callback) 0 76 76
238 $when_callback;
239  
240 # Callback will be the preferred thing
241 76 ▶ if ($when_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($when_callback) 20 56 76
242 ## no critic (ProhibitNoStrict);
243 404 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
244 20 return $when_callback->( $params, $self );
245 }
246  
247 56 my $any;
248 my @when_fields_value;
249  
250 56 ▶ if ($when_any_field) {
Branch: 1 branch
BranchTrueFalseTotal
if ($when_any_field) 10 46 56
251 10 ▶ croak "'any_field' is set to an empty list" if !@$when_any_field;
Branch: 1 branch
BranchTrueFalseTotal
unless @$when_any_field 0 10 10
252  
253 10 $any = 1;
254  
255 10 @$when_fields = @$when_any_field;
256 }
257  
258 56 ▶ if ($when_fields) {
Branch: 1 branch
BranchTrueFalseTotal
if ($when_fields) { } 17 39 56
259 17 ▶ croak "'fields' is set to an empty list" if !@$when_fields;
Branch: 1 branch
BranchTrueFalseTotal
unless @$when_fields 0 17 17
260  
261 17 for my $name (@$when_fields) {
262 54 my $value = $self->get_nested_hash_value( $params, $name );
263  
264 54 ▶ push @when_fields_value, $value
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 54 0 54
265 if defined $value;
266 }
267 }
268 else {
269  
270 # nothing to constrain if field doesn't exist
271 39 my $value = $self->get_nested_hash_value( $params, $when_field );
272  
273 39 ▶ push @when_fields_value, $value
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 39 0 39
274 if defined $value;
275 }
276  
277 56 ▶ DEBUG_CONSTRAINTS_WHEN
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS_WHEN() and debug("WHEN_FIELDS_VALUES", \@when_fields_value) 0 56 56
278 && debug( 'WHEN_FIELDS_VALUES' => \@when_fields_value );
279  
280 56 ▶ if ( !@when_fields_value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@when_fields_value) 0 56 56
281 0 ▶ DEBUG_CONSTRAINTS_WHEN
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS_WHEN() and debug("No 'when' fields values exist - returning false") 0 0 0
282 && debug("No 'when' fields values exist - returning false");
283 0 return 0;
284 }
285  
286 56 my @values;
287  
288 56 ▶ if ( defined( my $value = $when->{value} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $value = $when->{'value'})) { } 24 32 56
elsif (defined(my $values = $when->{'values'})) { } 10 22 32
289 24 push @values, $value;
290 }
291 elsif ( defined( my $values = $when->{values} ) ) {
292 10 push @values, @$values;
293 }
294  
295 # determine if condition is fulfilled
296 56 my @ok;
297  
298 56 ▶ if (@values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values) { } 34 22 56
299 34 for my $value (@when_fields_value) {
300 34 ▶ push @ok, any { $value eq $_ } @values;
Subroutine __ANON__: called CC=1 SCAR=0.0
301 }
302 }
303 else {
304 22 for my $value (@when_fields_value) {
305 59 ▶ push @ok, $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 29 30 59
306 }
307 }
308  
309 56 ▶ DEBUG_CONSTRAINTS_WHEN && debug( "'when' value matches" => \@ok );
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS_WHEN() and debug("'when' value matches", \@ok) 0 56 56
310  
311 my $return
312 = $any
313 21 ▶ ? any { $when->{not} ? !$_ : $_ } @ok
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$when->{'not'} ? : 0 21 21
314 56 ▶ : all { $when->{not} ? !$_ : $_ } @ok;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 2 branches
BranchTrueFalseTotal
$any ? : 10 46 56
$when->{'not'} ? : 14 35 49
315  
316 56 ▶ DEBUG_CONSTRAINTS_WHEN && debug( "'when' return value" => $return );
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS_WHEN() and debug("'when' return value", $return) 0 56 56
317  
318 56 return $return;
319 }
320  
321 sub fetch_error_message {
322 0 ▶ my ($self) = @_;
Subroutine fetch_error_message: not called CC=1 SCAR=6.9
Pod documented
323  
324 0 my $error = HTML::FormFu::Exception::Constraint->new(
325 { form => $self->form,
326 parent => $self->parent,
327 processor => $self,
328 } );
329  
330 0 return $error->message;
331 }
332  
333 sub clone {
334 51 ▶ my $self = shift;
Subroutine clone: called CC=2 SCAR=6.9
Pod undocumented
335  
336 51 my $clone = $self->SUPER::clone(@_);
337  
338 51 ▶ if ( defined( my $when = $self->when ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $when = $self->when)) 1 50 51
339 1 $clone->when( Clone::clone $when );
340 }
341  
342 51 return $clone;
343 }
344  
345 404 __PACKAGE__->meta->make_immutable;
346  
347 404 1;
348  
349 __END__
350  
351 =pod
352  
353 =encoding UTF-8
354  
355 =head1 NAME
356  
357 HTML::FormFu::Constraint - Constrain User Input
358  
359 =head1 VERSION
360  
361 version 2.06
362  
363 =head1 SYNOPSIS
364  
365 ---
366 elements:
367 - type: Text
368 name: foo
369 constraints:
370 - type: Length
371 min: 8
372 when:
373 field: bar
374 values: [ 1, 3, 5 ]
375 - type: Text
376 name: bar
377 constraints:
378 - Integer
379 - Required
380 constraints:
381 - SingleValue
382  
383 =head1 DESCRIPTION
384  
385 User input is processed in the following order:
386  
387 =over
388  
389 =item L<Filters|HTML::FormFu::Filter>
390  
391 =item L<Constraints|HTML::FormFu::Constraint>
392  
393 =item L<Inflators|HTML::FormFu::Inflator>
394  
395 =item L<Validators|HTML::FormFu::Validator>
396  
397 =item L<Transformers|HTML::FormFu::Transformer>
398  
399 =back
400  
401 See L<HTML::FormFu/"FORM LOGIC AND VALIDATION"> for further details.
402  
403 L<HTML::FormFu/constraints> can be called on any L<form|HTML::FormFu>,
404 L<block element|HTML::FormFu::Element::Block> (includes fieldsets) or
405 L<field element|HTML::FormFu::Role::Element::Field>.
406  
407 If called on a field element, no C<name> argument should be passed.
408  
409 If called on a L<form|HTML::FormFu> or
410 L<block element|HTML::FormFu::Element::Block>, if no C<name> argument is
411 provided, a new constraint is created for and added to every field on that
412 form or block.
413  
414 See L<HTML::FormFu/"FORM LOGIC AND VALIDATION"> for further details.
415  
416 =head1 METHODS
417  
418 =head2 type
419  
420 Returns the C<type> argument originally used to create the constraint.
421  
422 =head2 not
423  
424 If true, inverts the results of the constraint - such that input that would
425 otherwise fail will pass, and vise-versa.
426  
427 This value is ignored by some constraints - see the documentation for
428 individual constraints for details.
429  
430 =head2 only_on_reps
431  
432 Argument: \@repeatable_count
433  
434 For constraints added to fields within a
435 L<Repeatable|HTML::FormFu::Element::Repeatable> element, if C<only_on_reps>
436 is set, the constraint will only be run for fields whose
437 L<repeatable_count|HTML::FormFu::Role::Element::Field/repeatable_count>
438 matches one of these set values.
439  
440 Not available for the constraints listed in
441 L<HTML::FormFu::Element::Repeatable/"Unsupported Constraints">.
442  
443 =head2 message
444  
445 Arguments: $string
446  
447 Set the message which will be displayed if the constraint fails.
448  
449 =head2 message_xml
450  
451 Arguments: $string
452  
453 Variant of L</message> which ensures the value won't be XML-escaped.
454  
455 =head2 message_loc
456  
457 Arguments: $string
458  
459 Variant of L</message> which uses L<localize|HTML::FormFu/localize> to
460 create the message.
461  
462 =head2 localize_args
463  
464 Provide arguments that should be passed to L<localize|HTML::FormFu/localize>
465 to replace C<[_1]>, C<[_2]>, etc. in the localized string.
466  
467 =head2 force_errors
468  
469 See L<HTML::FormFu/force_errors> for details.
470  
471 =head2 parent
472  
473 Returns the L<field|HTML::FormFu::Role::Element::Field> object that the constraint
474 is associated with.
475  
476 =head2 form
477  
478 Returns the L<HTML::FormFu> object that the constraint's field is attached
479 to.
480  
481 =head2 name
482  
483 Shorthand for C<< $constraint->parent->name >>
484  
485 =head2 when
486  
487 Defines a condition for the constraint. Only when the condition is fulfilled
488 the constraint will be applied.
489  
490 This method expects a hashref.
491  
492 The C<field> or C<callback> must be supplied, all other fields are optional.
493  
494 If C<value> or C<values> is not supplied, the constraint will pass if the
495 named field's value is true.
496  
497 The following keys are supported:
498  
499 =over
500  
501 =item field
502  
503 Nested-name of form field that shall be checked against - if C<< when->{value} >>
504 is set, the C<when> condition passes if the named field's value matches that,
505 otherwise the C<when> condition passes if the named field's value is true.
506  
507 =item fields
508  
509 Array-ref of nested-names that shall be checked. The C<when> condition passes
510 if all named-fields' values pass, using the same rules as C<field> above.
511  
512 =item any_field
513  
514 Array-ref of nested-names that shall be checked. The C<when> condition passes
515 if any named-fields' values pass, using the same rules as C<field> above.
516  
517 =item value
518  
519 Expected value in the form field 'field'
520  
521 =item values
522  
523 Array of multiple values, one must match to fulfill the condition
524  
525 =item not
526  
527 Inverts the when condition - value(s) must not match
528  
529 =item callback
530  
531 A callback subroutine-reference or fully resolved subroutine name can be
532 supplied to perform complex checks. An hashref of all parameters is passed
533 to the callback sub. In this case all other keys are ignored, including C<not>.
534 You need to return a true value for the constraint to be applied or a false
535 value to not apply it.
536  
537 The callback subroutine receives 2 arguments:
538  
539 =over 8
540  
541 =item 1
542  
543 C<$params> (hashref of all submitted parameters)
544  
545 =item 2
546  
547 C<$constraint> (the Constraint object)
548  
549 =back
550  
551 =back
552  
553 =head1 EXPERIMENTAL METHODS
554  
555 =head2 fetch_error_message
556  
557 Return value: $string
558  
559 Attempt to return the error message that would be used if this constraint
560 generated an error.
561  
562 This will generally be correct for simple constraints with a fixed message or
563 which use a placeholder from a known value, such as
564 L<HTML::FormFu::Constraint::Min/min>.
565 This will generally C<not> return the correct message for constraints which
566 use L<HTML::FormFu::Role::Constraint::Others/others>, where the field with an
567 error is not known without actually fully processing a form submission.
568  
569 =head1 CORE CONSTRAINTS
570  
571 =over
572  
573 =item L<HTML::FormFu::Constraint::AllOrNone>
574  
575 =item L<HTML::FormFu::Constraint::ASCII>
576  
577 =item L<HTML::FormFu::Constraint::AutoSet>
578  
579 =item L<HTML::FormFu::Constraint::Bool>
580  
581 =item L<HTML::FormFu::Constraint::Callback>
582  
583 =item L<HTML::FormFu::Constraint::CallbackOnce>
584  
585 =item L<HTML::FormFu::Constraint::DateTime>
586  
587 =item L<HTML::FormFu::Constraint::DependOn>
588  
589 =item L<HTML::FormFu::Constraint::Email>
590  
591 =item L<HTML::FormFu::Constraint::Equal>
592  
593 =item L<HTML::FormFu::Constraint::File>
594  
595 =item L<HTML::FormFu::Constraint::File::MIME>
596  
597 =item L<HTML::FormFu::Constraint::File::MaxSize>
598  
599 =item L<HTML::FormFu::Constraint::File::MinSize>
600  
601 =item L<HTML::FormFu::Constraint::File::Size>
602  
603 =item L<HTML::FormFu::Constraint::Integer>
604  
605 =item L<HTML::FormFu::Constraint::JSON>
606  
607 =item L<HTML::FormFu::Constraint::Length>
608  
609 =item L<HTML::FormFu::Constraint::MaxLength>
610  
611 =item L<HTML::FormFu::Constraint::MaxRange>
612  
613 =item L<HTML::FormFu::Constraint::MinLength>
614  
615 =item L<HTML::FormFu::Constraint::MinRange>
616  
617 =item L<HTML::FormFu::Constraint::MinMaxFields>
618  
619 =item L<HTML::FormFu::Constraint::Number>
620  
621 =item L<HTML::FormFu::Constraint::Printable>
622  
623 =item L<HTML::FormFu::Constraint::Range>
624  
625 =item L<HTML::FormFu::Constraint::Regex>
626  
627 =item L<HTML::FormFu::Constraint::Required>
628  
629 =item L<HTML::FormFu::Constraint::Set>
630  
631 =item L<HTML::FormFu::Constraint::SingleValue>
632  
633 =item L<HTML::FormFu::Constraint::Word>
634  
635 =back
636  
637 =head1 NON-CORE CONSTRAINTS AVAILABLE ON CPAN
638  
639 =over
640  
641 =item L<HTML::FormFu::Constraint::reCAPTCHA>
642  
643 =back
644  
645 =head1 CAVEATS
646  
647 See L<HTML::FormFu::Element::Repeatable/"Unsupported Constraints">
648 for a list of constraints that won't work within
649 L<HTML::FormFu::Element::Repeatable>.
650  
651 =head1 AUTHOR
652  
653 Carl Franks, C<cfranks@cpan.org>
654  
655 Based on the original source code of L<HTML::Widget::Constraint>, by
656 Sebastian Riedel, C<sri@oook.de>.
657  
658 =head1 LICENSE
659  
660 This library is free software, you can redistribute it and/or modify it under
661 the same terms as Perl itself.
662  
663 =head1 AUTHOR
664  
665 Carl Franks <cpan@fireartist.com>
666  
667 =head1 COPYRIGHT AND LICENSE
668  
669 This software is copyright (c) 2018 by Carl Franks.
670  
671 This is free software; you can redistribute it and/or modify it under
672 the same terms as the Perl 5 programming language system itself.
673  
674 =cut
« lib/HTML/FormFu/Role/Element/SingleValueField.pm ↑ Summary lib/HTML/FormFu/Role/Element/Group.pm »