« lib/HTML/FormFu/Role/Element/ProcessOptionsFromModel.pm
↑ Summary
lib/HTML/FormFu/Role/Element/Coercible.pm »
| 1 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package HTML::FormFu::Validator; | ||||||||||||||||||||||||||||
| 4 | 4 | $HTML::FormFu::Validator::VERSION = '2.06'; | |||||||||||||||||||||||||||
| 5 | # ABSTRACT: Validator Base Class | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | extends 'HTML::FormFu::Processor'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use HTML::FormFu::Exception::Validator; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use Scalar::Util qw( reftype blessed ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | sub process { | ||||||||||||||||||||||||||||
| 15 | 8 | ▶ | my ( $self, $params ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
process: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 8 | my $value = $self->get_nested_hash_value( $params, $self->nested_name ); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 8 | my @errors; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 8 | ▶ | if ( ( reftype($value) || '' ) eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
builtin::reftype($value) || ''
MC/DC100%1/1
builtin::reftype($value) || ''
builtin::reftype($value)
Truth table100%2/2
A
A= builtin::reftype($value)
| |||||||||||||||||||||||||||||
| 22 | 2 | push @errors, eval { $self->validate_values( $value, $params ) }; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 2 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 0 | push @errors, $self->return_error($@); | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | else { | ||||||||||||||||||||||||||||
| 29 | 6 | my $ok = eval { $self->validate_value( $value, $params ) }; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 6 | ▶ | if ( $@ or !$ok ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$@ or not $ok
MC/DC50%1/2
$@ or not $ok
$@
not $ok
Truth table66%2/3
A or not(B)
A= $@B= not $ok
| |||||||||||||||||||||||||||||
| 32 | 1 | push @errors, $self->return_error($@); | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 8 | return @errors; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | sub validate_values { | ||||||||||||||||||||||||||||
| 40 | 2 | ▶ | my ( $self, $values, $params ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
validate_values: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 2 | my @errors; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 2 | for my $value (@$values) { | |||||||||||||||||||||||||||
| 45 | 6 | my $ok = eval { $self->validate_value( $value, $params ) }; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 6 | ▶ | push @errors, $self->return_error($@) if !$ok; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 2 | return @errors; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | sub validate_value { | ||||||||||||||||||||||||||||
| 54 | 0 | ▶ | croak "validate() should be overridden"; | ||||||||||||||||||||||||||
|
Subroutine
validate_value: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | sub return_error { | ||||||||||||||||||||||||||||
| 58 | 1 | ▶ | my ( $self, $err ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
return_error: called CC=4 SCAR=18.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 1 | ▶ | if ( !blessed $err || !$err->isa('HTML::FormFu::Exception::Validator') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($err) and $err->isa("HTML::FormFu::Exception::Validator")
MC/DC0%0/2
builtin::blessed($err) and $err->isa("HTML::FormFu::Exception::Validator")
builtin::blessed($err)
$err->isa("HTML::FormFu::Exception::Validator")
Truth table33%1/3
A and B
A= builtin::blessed($err)B= $err->isa("HTML::FormFu::Exception::Validator")
| |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | $err = HTML::FormFu::Exception::Validator->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | $err ? { message => $err } : () ); | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 1 | return $err; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 4 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 4 | 1; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | __END__ | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =pod | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =head1 NAME | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | HTML::FormFu::Validator - Validator Base Class | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =head1 VERSION | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | version 2.06 | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =head1 METHODS | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =head1 CORE VALIDATORS | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =over | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =item L<HTML::FormFu::Validator::Callback> | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =back | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =head1 BEST PRACTICES | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | Try to avoid using callbacks if possible. Below is a more maintainable | ||||||||||||||||||||||||||||
| 103 | and reusable approach, which also keeps the code out of the controller. | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | A normal application's directory would contain: | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | lib/HTML/FormFu/Constraint/MyApp/ | ||||||||||||||||||||||||||||
| 108 | lib/HTML/FormFu/Validator/MyApp/ | ||||||||||||||||||||||||||||
| 109 | lib/HTML/FormFu/Plugin/MyApp/ | ||||||||||||||||||||||||||||
| 110 | etc. | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | Then, the form config file would just need: | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | validator: 'MyApp::SomeValidator' | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | And the class would be something like this: | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | package HTML::FormFu::Validator::MyApp::SomeValidator; | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | use Moose; | ||||||||||||||||||||||||||||
| 121 | extends 'HTML::FormFu::Validator'; | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | sub validate_value { | ||||||||||||||||||||||||||||
| 124 | my ( $self, $value, $params ) = @_; | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | my $c = $self->form->stash->{context}; | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | return 1 if $c->model('DBIC')->is_valid($value); | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # assuming you want to return a custom error message | ||||||||||||||||||||||||||||
| 131 | # which perhaps includes something retrieved from the model | ||||||||||||||||||||||||||||
| 132 | # otherwise, just return 0 | ||||||||||||||||||||||||||||
| 133 | die HTML::FormFu::Exception::Validator->new({ | ||||||||||||||||||||||||||||
| 134 | message => 'custom error message', | ||||||||||||||||||||||||||||
| 135 | }); | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 1; | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Carl Franks, C<cfranks@cpan.org> | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | This library is free software, you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 147 | the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | Carl Franks <cpan@fireartist.com> | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | This software is copyright (c) 2018 by Carl Franks. | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 158 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =cut | ||||||||||||||||||||||||||||