| 1 |
package HTML::FormHandler::Validate; |
| 2 |
# ABSTRACT: validation role (internal) |
| 3 |
$HTML::FormHandler::Validate::VERSION = '0.410002'; |
| 4 |
|
| 5 |
use Moose::Role; |
| 6 |
use Carp; |
| 7 |
|
| 8 |
has 'required' => ( isa => 'Bool', is => 'rw', default => '0' ); |
| 9 |
has 'required_when' => ( is => 'rw', isa => 'HashRef', predicate => 'has_required_when' ); |
| 10 |
has 'required_message' => ( |
| 11 |
isa => 'ArrayRef|Str', |
| 12 |
is => 'rw', |
| 13 |
); |
| 14 |
has 'unique' => ( isa => 'Bool', is => 'rw', predicate => 'has_unique' ); |
| 15 |
has 'unique_message' => ( isa => 'Str', is => 'rw' ); |
| 16 |
has 'range_start' => ( isa => 'Int|Undef', is => 'rw' ); |
| 17 |
has 'range_end' => ( isa => 'Int|Undef', is => 'rw' ); |
| 18 |
|
| 19 |
sub test_ranges { |
| 20 |
my $field = shift; |
| 21 |
return 1 if $field->can('options') || $field->has_errors; |
| 22 |
|
| 23 |
my $value = $field->value; |
| 24 |
|
| 25 |
return 1 unless defined $value; |
| 26 |
|
| 27 |
my $low = $field->range_start; |
| 28 |
my $high = $field->range_end; |
| 29 |
|
| 30 |
if ( defined $low && defined $high ) { |
| 31 |
return |
| 32 |
$value >= $low && $value <= $high ? 1 : |
| 33 |
$field->add_error( $field->get_message('range_incorrect'), $low, $high ); |
| 34 |
} |
| 35 |
|
| 36 |
if ( defined $low ) { |
| 37 |
return |
| 38 |
$value >= $low ? 1 : |
| 39 |
$field->add_error( $field->get_message('range_too_low'), $low ); |
| 40 |
} |
| 41 |
|
| 42 |
if ( defined $high ) { |
| 43 |
return |
| 44 |
$value <= $high ? 1 : |
| 45 |
$field->add_error( $field->get_message('range_too_high'), $high ); |
| 46 |
} |
| 47 |
|
| 48 |
return 1; |
| 49 |
} |
| 50 |
|
| 51 |
sub validate_field { |
| 52 |
my $field = shift; |
| 53 |
|
| 54 |
return unless $field->has_result; |
| 55 |
$field->clear_errors; # this is only here for testing convenience |
| 56 |
|
| 57 |
# if the 'fields_for_input_without_param' flag is set, and the field doesn't have input, |
| 58 |
# copy the value to the input. |
| 59 |
if ( !$field->has_input && $field->form && $field->form->use_fields_for_input_without_param ) { |
| 60 |
$field->result->_set_input($field->value); |
| 61 |
} |
| 62 |
# handle required and required_when processing, and transfer input to value |
| 63 |
my $continue_validation = 1; |
| 64 |
if ( ( $field->required || |
| 65 |
( $field->has_required_when && $field->match_when($field->required_when) ) ) && |
| 66 |
( !$field->has_input || !$field->input_defined ) ) { |
| 67 |
$field->missing(1); |
| 68 |
$field->add_error( $field->get_message('required'), $field->loc_label ); |
| 69 |
if( $field->has_input ) { |
| 70 |
$field->not_nullable ? $field->_set_value($field->input) : $field->_set_value(undef); |
| 71 |
} |
| 72 |
$continue_validation = 0; |
| 73 |
} |
| 74 |
elsif ( $field->DOES('HTML::FormHandler::Field::Repeatable') ) { } |
| 75 |
elsif ( !$field->has_input ) { |
| 76 |
$continue_validation = 0; |
| 77 |
} |
| 78 |
elsif ( !$field->input_defined ) { |
| 79 |
if ( $field->not_nullable ) { |
| 80 |
$field->_set_value($field->input); |
| 81 |
# handles the case where a compound field value needs to have empty subfields |
| 82 |
$continue_validation = 0 unless $field->has_flag('is_compound'); |
| 83 |
} |
| 84 |
elsif ( $field->no_value_if_empty || $field->has_flag('is_contains') ) { |
| 85 |
$continue_validation = 0; |
| 86 |
} |
| 87 |
else { |
| 88 |
$field->_set_value(undef); |
| 89 |
$continue_validation = 0; |
| 90 |
} |
| 91 |
} |
| 92 |
return if ( !$continue_validation && !$field->validate_when_empty ); |
| 93 |
|
| 94 |
# do building of node |
| 95 |
if ( $field->DOES('HTML::FormHandler::Fields') ) { |
| 96 |
$field->_fields_validate; |
| 97 |
} |
| 98 |
else { |
| 99 |
my $input = $field->input; |
| 100 |
$input = $field->inflate( $input ) if $field->has_inflate_method; |
| 101 |
$field->_set_value( $input ); |
| 102 |
} |
| 103 |
|
| 104 |
$field->_inner_validate_field(); |
| 105 |
$field->_apply_actions; |
| 106 |
$field->validate( $field->value ); |
| 107 |
$field->test_ranges; |
| 108 |
$field->_validate($field) # form field validation method |
| 109 |
if ( $field->has_value && defined $field->value ); |
| 110 |
# validation done, if everything validated, do deflate_value for |
| 111 |
# final $form->value |
| 112 |
if( $field->has_deflate_value_method && !$field->has_errors ) { |
| 113 |
$field->_set_value( $field->deflate_value($field->value) ); |
| 114 |
} |
| 115 |
|
| 116 |
return !$field->has_errors; |
| 117 |
} |
| 118 |
|
| 119 |
sub _inner_validate_field { } |
| 120 |
|
| 121 |
sub validate { 1 } |
| 122 |
|
| 123 |
has 'actions' => ( |
| 124 |
traits => ['Array'], |
| 125 |
isa => 'ArrayRef', |
| 126 |
is => 'rw', |
| 127 |
default => sub { [] }, |
| 128 |
handles => { |
| 129 |
add_action => 'push', |
| 130 |
num_actions =>'count', |
| 131 |
has_actions => 'count', |
| 132 |
clear_actions => 'clear', |
| 133 |
} |
| 134 |
); |
| 135 |
|
| 136 |
sub _build_apply_list { |
| 137 |
my $self = shift; |
| 138 |
my @apply_list; |
| 139 |
foreach my $sc ( reverse $self->meta->linearized_isa ) { |
| 140 |
my $meta = $sc->meta; |
| 141 |
if ( $meta->can('calculate_all_roles') ) { |
| 142 |
foreach my $role ( $meta->calculate_all_roles ) { |
| 143 |
if ( $role->can('apply_list') && $role->has_apply_list ) { |
| 144 |
foreach my $apply_def ( @{ $role->apply_list } ) { |
| 145 |
my %new_apply = %{$apply_def}; # copy hashref |
| 146 |
push @apply_list, \%new_apply; |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
if ( $meta->can('apply_list') && $meta->has_apply_list ) { |
| 152 |
foreach my $apply_def ( @{ $meta->apply_list } ) { |
| 153 |
my %new_apply = %{$apply_def}; # copy hashref |
| 154 |
push @apply_list, \%new_apply; |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
$self->add_action(@apply_list); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
# Locale::Maketext compiles its FORMAT argument: a '[...]' group in it becomes |
| 163 |
# method-dispatch code (see _compile in Locale::Maketext). Messages FormHandler |
| 164 |
# itself authors are templates on purpose, but several kinds of text reaching |
| 165 |
# _apply_actions are not ours and do carry request data -- a warning trapped by |
| 166 |
# the $SIG{__WARN__} handler below (Perl quotes the offending value into it |
| 167 |
# verbatim), a type constraint's failure message (the type system renders a |
| 168 |
# rejected reference in bracket-and-comma form), and exceptions from a coercion |
| 169 |
# or a transform. |
| 170 |
# |
| 171 |
# Rather than escape the bracket metacharacters in such text, hand it to the |
| 172 |
# localizer as an ARGUMENT with a constant format, which is the idiom the |
| 173 |
# add_error POD recommends to applications: the text then cannot be parsed as |
| 174 |
# anything at all, so there is no escaping to get right. |
| 175 |
# |
| 176 |
# This applies to all such text, not only text that happens to contain a |
| 177 |
# bracket. Two reasons. It keeps the rule simple enough to check by reading it |
| 178 |
# -- "text we did not author is never a format" -- with no predicate to get |
| 179 |
# wrong. And it stops the text being used as a LEXICON KEY: with _AUTO set, as |
| 180 |
# it is for HTML::FormHandler::I18N::en_us, Locale::Maketext memoises every |
| 181 |
# distinct format into a package-global hash that is never evicted and outlives |
| 182 |
# the handle, so distinct submitted values otherwise accumulate in a long-lived |
| 183 |
# process for as long as it runs. |
| 184 |
# |
| 185 |
# The cost is that such text is no longer looked up in the lexicon, so a |
| 186 |
# translated custom type-constraint message is now shown as the type gave it. |
| 187 |
# A maintainer who would rather keep that lookup can return false for text with |
| 188 |
# no '~', '[' or ']' in it, which is the complete set of characters |
| 189 |
# Locale::Maketext's compiler treats specially: |
| 190 |
# |
| 191 |
# return 0 if $text !~ /[~\[\]]/; |
| 192 |
# |
| 193 |
# and adjust the corresponding assertion in t/errors/maketext_inert_argument.t. |
| 194 |
# References are diverted too, and deliberately so. All of the sources above can |
| 195 |
# deliver one: `warn $object` hands the object itself to $SIG{__WARN__}, `die |
| 196 |
# $object` in a transform or a coercion leaves it in $@, and a type constraint's |
| 197 |
# message block may return one. Locale::Maketext then STRINGIFIES whatever it is |
| 198 |
# given as the format -- _compile's own fast-path regex does it before anything |
| 199 |
# else -- so an object with a '""' overload that yields bracket notation would |
| 200 |
# skip a reference bailout and be compiled anyway. Testing the stringification |
| 201 |
# here instead would not help: Maketext performs its own, later stringification, |
| 202 |
# so inspecting one and compiling the other leaves a gap, and an overload is |
| 203 |
# arbitrary code that need not answer the same way twice. Passing the object as |
| 204 |
# an argument sidesteps all of it -- an argument is interpolated, never compiled, |
| 205 |
# so it stringifies exactly as it did before and only the compilation is gone. |
| 206 |
sub _needs_inert_format { |
| 207 |
my ( $self, $text ) = @_; |
| 208 |
|
| 209 |
return defined $text ? 1 : 0; |
| 210 |
} |
| 211 |
sub _apply_actions { |
| 212 |
my $self = shift; |
| 213 |
|
| 214 |
my $error_message; |
| 215 |
# True when $error_message holds text this module did not author. |
| 216 |
my $foreign_message; |
| 217 |
local $SIG{__WARN__} = sub { |
| 218 |
my $error = shift; |
| 219 |
$error_message = $error; |
| 220 |
$foreign_message = 1; |
| 221 |
return 1; |
| 222 |
}; |
| 223 |
|
| 224 |
my $is_type = sub { |
| 225 |
my $class = blessed shift or return; |
| 226 |
return $class eq 'MooseX::Types::TypeDecorator' || $class->isa('Type::Tiny'); |
| 227 |
}; |
| 228 |
|
| 229 |
for my $action ( @{ $self->actions || [] } ) { |
| 230 |
$error_message = undef; |
| 231 |
$foreign_message = undef; |
| 232 |
# the first time through value == input |
| 233 |
my $value = $self->value; |
| 234 |
my $new_value = $value; |
| 235 |
# Moose constraints |
| 236 |
if ( !ref $action || $is_type->($action) ) { |
| 237 |
$action = { type => $action }; |
| 238 |
} |
| 239 |
if ( my $when = $action->{when} ) { |
| 240 |
next unless $self->match_when($when); |
| 241 |
} |
| 242 |
if ( exists $action->{type} ) { |
| 243 |
my $tobj; |
| 244 |
if ( $is_type->($action->{type}) ) { |
| 245 |
$tobj = $action->{type}; |
| 246 |
} |
| 247 |
else { |
| 248 |
my $type = $action->{type}; |
| 249 |
$tobj = Moose::Util::TypeConstraints::find_type_constraint($type) or |
| 250 |
die "Cannot find type constraint $type"; |
| 251 |
} |
| 252 |
if ( $tobj->has_coercion && $tobj->validate($value) ) { |
| 253 |
my $coerce_returned = eval { $tobj->coerce($value) }; |
| 254 |
if ($@) { |
| 255 |
if ( $tobj->has_message ) { |
| 256 |
$error_message = $tobj->message->($value); |
| 257 |
} |
| 258 |
else { |
| 259 |
$error_message = $@; |
| 260 |
} |
| 261 |
$foreign_message = 1; |
| 262 |
} |
| 263 |
else { |
| 264 |
$new_value = $coerce_returned; |
| 265 |
$self->_set_value($new_value); |
| 266 |
} |
| 267 |
|
| 268 |
} |
| 269 |
unless ($error_message) { |
| 270 |
$error_message = $tobj->validate($new_value); |
| 271 |
$foreign_message = 1 if $error_message; |
| 272 |
} |
| 273 |
} |
| 274 |
elsif ( ref $action->{check} eq 'CODE' ) { |
| 275 |
if ( !$action->{check}->($value, $self) ) { |
| 276 |
$error_message = $self->get_message('wrong_value'); |
| 277 |
} |
| 278 |
} |
| 279 |
elsif ( ref $action->{check} eq 'Regexp' ) { |
| 280 |
if ( $value !~ $action->{check} ) { |
| 281 |
$error_message = [$self->get_message('no_match'), $value]; |
| 282 |
} |
| 283 |
} |
| 284 |
elsif ( ref $action->{check} eq 'ARRAY' ) { |
| 285 |
if ( !grep { $value eq $_ } @{ $action->{check} } ) { |
| 286 |
$error_message = [$self->get_message('not_allowed'), $value]; |
| 287 |
} |
| 288 |
} |
| 289 |
elsif ( ref $action->{transform} eq 'CODE' ) { |
| 290 |
$new_value = eval { |
| 291 |
no warnings 'all'; |
| 292 |
$action->{transform}->($value, $self); |
| 293 |
}; |
| 294 |
if ($@) { |
| 295 |
$error_message = $@ || $self->get_message('error_occurred'); |
| 296 |
$foreign_message = 1; |
| 297 |
} |
| 298 |
else { |
| 299 |
$self->_set_value($new_value); |
| 300 |
} |
| 301 |
} |
| 302 |
if ( defined $error_message ) { |
| 303 |
my @message = ref $error_message eq 'ARRAY' ? @$error_message : ($error_message); |
| 304 |
if ( defined $action->{message} ) { |
| 305 |
my $act_msg = $action->{message}; |
| 306 |
if ( ref $act_msg eq 'CODE' ) { |
| 307 |
$act_msg = $act_msg->($value, $self, $error_message); |
| 308 |
} |
| 309 |
if ( ref $act_msg eq 'ARRAY' ) { |
| 310 |
@message = @{$act_msg}; |
| 311 |
} |
| 312 |
elsif ( ref \$act_msg eq 'SCALAR' ) { |
| 313 |
@message = ($act_msg); |
| 314 |
} |
| 315 |
# The application supplied this message, so it is a template on |
| 316 |
# purpose and foreignness no longer applies. |
| 317 |
$foreign_message = 0; |
| 318 |
} |
| 319 |
if ( $foreign_message && $self->_needs_inert_format( $message[0] ) ) { |
| 320 |
# Interpolate explicitly. A format that is a single bracket group |
| 321 |
# compiles to one chunk, and Locale::Maketext only prepends its |
| 322 |
# `join ''` when there is more than one, so '[_1]' alone returns the |
| 323 |
# argument UNCHANGED rather than a string -- which would put an |
| 324 |
# exception object into the errors attribute, where the type |
| 325 |
# constraint is ArrayRef[Str]. Stringifying here also means the |
| 326 |
# conversion happens exactly once, under our control, and its result |
| 327 |
# can never be reparsed as a format. |
| 328 |
@message = ( '[_1]', "$message[0]" ); |
| 329 |
} |
| 330 |
$self->add_error(@message); |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
sub match_when { |
| 336 |
my ( $self, $when ) = @_; |
| 337 |
|
| 338 |
my $matched = 0; |
| 339 |
foreach my $key ( keys %$when ) { |
| 340 |
my $check_against = $when->{$key}; |
| 341 |
my $from_form = ( $key =~ /^\+/ ); |
| 342 |
$key =~ s/^\+//; |
| 343 |
my $field = $from_form ? $self->form->field($key) : $self->parent->subfield( $key ); |
| 344 |
unless ( $field ) { |
| 345 |
warn "field '$key' not found processing 'when' for '" . $self->full_name . "'"; |
| 346 |
next; |
| 347 |
} |
| 348 |
my $field_fif = defined $field->fif ? $field->fif : ''; |
| 349 |
if ( ref $check_against eq 'CODE' ) { |
| 350 |
$matched++ |
| 351 |
if $check_against->($field_fif, $self); |
| 352 |
} |
| 353 |
elsif ( ref $check_against eq 'ARRAY' ) { |
| 354 |
foreach my $value ( @$check_against ) { |
| 355 |
$matched++ if ( $value eq $field_fif ); |
| 356 |
} |
| 357 |
} |
| 358 |
elsif ( $check_against eq $field_fif ) { |
| 359 |
$matched++; |
| 360 |
} |
| 361 |
else { |
| 362 |
$matched = 0; |
| 363 |
last; |
| 364 |
} |
| 365 |
} |
| 366 |
return $matched; |
| 367 |
} |
| 368 |
|
| 369 |
use namespace::autoclean; |
| 370 |
1; |
| 371 |
|
| 372 |
__END__ |
| 373 |
|
| 374 |
=pod |
| 375 |
|
| 376 |
=encoding UTF-8 |
| 377 |
|
| 378 |
=head1 NAME |
| 379 |
|
| 380 |
HTML::FormHandler::Validate - validation role (internal) |
| 381 |
|
| 382 |
=head1 VERSION |
| 383 |
|
| 384 |
version 0.410002 |
| 385 |
|
| 386 |
=head1 SYNOPSIS |
| 387 |
|
| 388 |
This is a role that contains validation and transformation code |
| 389 |
used by L<HTML::FormHandler::Field>. |
| 390 |
|
| 391 |
=head1 AUTHOR |
| 392 |
|
| 393 |
FormHandler Contributors - see HTML::FormHandler |
| 394 |
|
| 395 |
=head1 COPYRIGHT AND LICENSE |
| 396 |
|
| 397 |
This software is copyright (c) 2017 by Gerda Shank. |
| 398 |
|
| 399 |
This is free software; you can redistribute it and/or modify it under |
| 400 |
the same terms as the Perl 5 programming language system itself. |
| 401 |
|
| 402 |
=cut |