| 1 |
package HTML::FormHandler::Manual::Errors; |
| 2 |
# ABSTRACT: FormHandler error methods |
| 3 |
|
| 4 |
__END__ |
| 5 |
|
| 6 |
=pod |
| 7 |
|
| 8 |
=encoding UTF-8 |
| 9 |
|
| 10 |
=head1 NAME |
| 11 |
|
| 12 |
HTML::FormHandler::Manual::Errors - FormHandler error methods |
| 13 |
|
| 14 |
=head1 VERSION |
| 15 |
|
| 16 |
version 0.410002 |
| 17 |
|
| 18 |
=head1 SYNOPSIS |
| 19 |
|
| 20 |
L<Manual Index|HTML::FormHandler::Manual> |
| 21 |
|
| 22 |
Errors and error messages for L<HTML::FormHandler>. |
| 23 |
|
| 24 |
=head1 DESCRIPTION |
| 25 |
|
| 26 |
Errors are added to field or form objects by the field 'add_error' method |
| 27 |
or the form 'add_form_error' method. FormHandler will perform the 'add_error' |
| 28 |
for you for built-in validation or 'apply' actions. When performing your |
| 29 |
own validation in a validation method, you must do the 'add_error' |
| 30 |
yourself. |
| 31 |
|
| 32 |
Errors, along with 'input' and 'value' attributes, are collected in the |
| 33 |
FormHandler 'result' objects. A number of error retrieving methods are |
| 34 |
delegated to the field and form classes. |
| 35 |
|
| 36 |
The existence (or not) of errors determines whether or not the form has |
| 37 |
been 'validated'. |
| 38 |
|
| 39 |
=head1 Form methods |
| 40 |
|
| 41 |
=over 4 |
| 42 |
|
| 43 |
=item errors |
| 44 |
|
| 45 |
Returns an array of localized error strings (both field and |
| 46 |
form errors): |
| 47 |
|
| 48 |
my @errors = $form->errors; |
| 49 |
|
| 50 |
Note: this is a form method, not a result method. For the same thing |
| 51 |
from a result object, use C<< $result->form_and_field_errors >>. |
| 52 |
|
| 53 |
=item has_errors |
| 54 |
|
| 55 |
Both 'form' errors and errors from the tree of subfields |
| 56 |
|
| 57 |
if( $form->has_errors ) { |
| 58 |
<do something> |
| 59 |
} |
| 60 |
|
| 61 |
=item form_errors, all_form_errors |
| 62 |
|
| 63 |
Returns an arrayref / array of error strings on the form (not including |
| 64 |
field errors). |
| 65 |
|
| 66 |
foreach my $err ( $self->all_form_errors ) { |
| 67 |
$output .= "<span class="error">$err</span>"; |
| 68 |
} |
| 69 |
|
| 70 |
=item has_form_errors |
| 71 |
|
| 72 |
Does the form have form_errors? |
| 73 |
|
| 74 |
=item add_form_error |
| 75 |
|
| 76 |
Add an error to the form which is not associated with a specific field. |
| 77 |
|
| 78 |
sub validate { |
| 79 |
my $self = shift; |
| 80 |
unless( <some condition> ) { |
| 81 |
$self->add_form_error('....'); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
=item push_form_errors |
| 86 |
|
| 87 |
Add a non-localized error to the form. |
| 88 |
|
| 89 |
=back |
| 90 |
|
| 91 |
=head1 Field methods |
| 92 |
|
| 93 |
The most common error method is probably 'add_error', which you |
| 94 |
use in the validation process. |
| 95 |
|
| 96 |
sub validate_foo { |
| 97 |
my ( $self, $field ) = @_; |
| 98 |
unless ( <some_condition> ) { |
| 99 |
$field->add_error('Error condition'); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
=over 4 |
| 104 |
|
| 105 |
=item errors |
| 106 |
|
| 107 |
Returns an array of error strings. |
| 108 |
|
| 109 |
=item has_errors |
| 110 |
|
| 111 |
Does the field have errors? Note that a compound field that contains subfields |
| 112 |
with errors will not return true for this method. If you want to know if there |
| 113 |
are errors in the subfields, do 'has_error_fields'. |
| 114 |
|
| 115 |
=item num_errors |
| 116 |
|
| 117 |
=item add_error |
| 118 |
|
| 119 |
Add an error to the field. Localization is performed. |
| 120 |
|
| 121 |
=item push_errors |
| 122 |
|
| 123 |
Add an error without localization. |
| 124 |
|
| 125 |
=item error_fields |
| 126 |
|
| 127 |
In a compound field (and its subclasses, like 'Repeatable'), the list |
| 128 |
of fields with errors. |
| 129 |
|
| 130 |
=back |
| 131 |
|
| 132 |
=head1 Result methods |
| 133 |
|
| 134 |
The input, value, and error attributes are actually stored in the |
| 135 |
result objects. Although most of the methods are delegated to the |
| 136 |
form and field classes, there are times, such as when rendering (because you might |
| 137 |
be rendering a result that's been peeled off of the form object), that |
| 138 |
you may need to use result methods. |
| 139 |
|
| 140 |
These are the main methods that you might need to use. |
| 141 |
|
| 142 |
=over 4 |
| 143 |
|
| 144 |
=item has_errors |
| 145 |
|
| 146 |
=item errors |
| 147 |
|
| 148 |
=item error_results |
| 149 |
|
| 150 |
The results with errors; 'error_fields' is a wrapper around this. |
| 151 |
|
| 152 |
=back |
| 153 |
|
| 154 |
=head1 Messages |
| 155 |
|
| 156 |
The base field class and the field subclasses have some 'built-in' error messages. |
| 157 |
These can be modified by setting the 'messages' hashref in the form or the |
| 158 |
individual fields. |
| 159 |
|
| 160 |
When a message is retrieved in a field with C<< $field->get_message('upload_file_') >> |
| 161 |
for example, the 'get_message' method will look first in user-set field specific messages, |
| 162 |
then in user-supplied form messages, finally in messages provided by the field classes. |
| 163 |
|
| 164 |
package MyApp::Form; |
| 165 |
use HTML::FormHandler::Moose; |
| 166 |
extends 'HTML::FormHandler'; |
| 167 |
|
| 168 |
sub build_messages { |
| 169 |
return { required => '....', my_message => '....' }; |
| 170 |
} |
| 171 |
... |
| 172 |
my $form = MyApp::Form->new( messages => { required => '...', ...} ); |
| 173 |
... |
| 174 |
has_field 'my_field' => ( messages => { required => 'Please provide a my_field' }, |
| 175 |
required => 1 ); |
| 176 |
|
| 177 |
=head1 AUTHOR |
| 178 |
|
| 179 |
FormHandler Contributors - see HTML::FormHandler |
| 180 |
|
| 181 |
=head1 COPYRIGHT AND LICENSE |
| 182 |
|
| 183 |
This software is copyright (c) 2017 by Gerda Shank. |
| 184 |
|
| 185 |
This is free software; you can redistribute it and/or modify it under |
| 186 |
the same terms as the Perl 5 programming language system itself. |
| 187 |
|
| 188 |
=cut |