Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add errors_by_name and errors_by_id to result

  • Loading branch information...
commit 3ad944f6f160f9ad228db0f7a661f9b04900d8e0 1 parent 8284ae7
@gshank authored
View
2  lib/HTML/FormHandler/Manual/Validation.pod
@@ -30,7 +30,7 @@ You need to set 'required' all the way up, if that's the behavior that you
want.
If a field is empty and *not* required, no other field validation will be
-performed unless the 'validate_when_empty' flag (see below) is set. The form's
+performed unless the 'validate_when_empty' flag (see below) is set. The form's
'validate' method, however, will always be called.
There is also the 'required_when' attribute, which works the same way as the
View
14 lib/HTML/FormHandler/Result/Role.pm
@@ -57,6 +57,20 @@ has 'error_results' => (
}
);
+sub errors_by_id {
+ my $self = shift;
+ my %errors;
+ $errors{$_->field_def->id} = [$_->all_errors] for $self->all_error_results;
+ return \%errors;
+}
+
+sub errors_by_name {
+ my $self = shift;
+ my %errors;
+ $errors{$_->field_def->html_name} = [$_->all_errors] for $self->all_error_results;
+ return \%errors;
+}
+
has 'errors' => (
traits => ['Array'],
is => 'rw',
View
2  t/result/errors.t
@@ -22,6 +22,8 @@ ok( !$result->validated, 'did not validate' );
ok( $result->field('foo_required')->has_errors, 'foo has error' );
ok( $result->has_errors, 'result has errors' );
is( $result->num_errors, 1, 'number of errors is correct' );
+is_deeply( $result->errors_by_name, { foo_required => ['Foo required field is required'] },
+ 'correct errors_by_name' );
is( $result->errors->[0], 'Foo required field is required', 'result field has error' );
is_html( $result->field('foo_required')->render, '
<div class="error">
Please sign in to comment.
Something went wrong with that request. Please try again.