Mojolicious-Plugin-FormFields
view release on metacpan or search on metacpan
<div id="<%= dom_id($addr->object) %>">
<h3>Address #<%= $addr->index + 1 %></h3>
<%= $addr->hidden('id') %>
...
</div>
<% } %>
=head1 VALIDATING & FILTERING
Validation rules are created by calling validation and/or filter methods
on the field to be validated
# In your controller
my $self = shift;
$self->field('user.name')->is_required;
$self->field('user.name')->filter('trim');
These methods can be chained
$self->field('user.name')->is_required->filter('trim');
To perform validation on a field call its C<valid> method
$field = $self->field('user.name');
$field->is_required;
$field->valid;
$field->error;
This will only validate and return the error for the C<user.name> field. To validate all fields and retrieve all error messages call the controller's C<valid> and C<errors> methods
$self->field('user.name')->is_required;
$self->field('user.age')->is_like(qr/^\d+$/);
$self->valid;
my $errors = $self->errors;
$errors->{'user.name'}
# ...
Of course the C<error>/C<errors> and C<valid> methods can be used in your view too
lib/Mojolicious/Plugin/FormFields.pm view on Meta::CPAN
filters => $self->{filters}
};
# A bit of massaging For the is_equal() validation
my $eq = $self->{eq_to_field};
if($eq) {
$field->{$eq} = $self->{c}->param($eq);
push @{$rules->{fields}}, $eq;
}
$result = Validate::Tiny::validate($field, $rules);
$self->{c}->param($name, $result->{data}->{$name}) if @{$self->{filters}};
$self->{result} = $result;
$result->{success};
}
sub is_equal
{
my $self = shift;
$self->{eq_to_field} = $_[0];
lib/Mojolicious/Plugin/FormFields.pm view on Meta::CPAN
<div id="<%= dom_id($addr->object) %>">
<h3>Address #<%= $addr->index + 1 %></h3>
<%= $addr->hidden('id') %>
...
</div>
<% } %>
=head1 VALIDATING & FILTERING
Validation rules are created by calling validation and/or filter methods
on the field to be validated
# In your controller
my $self = shift;
$self->field('user.name')->is_required;
$self->field('user.name')->filter('trim');
These methods can be chained
$self->field('user.name')->is_required->filter('trim');
To perform validation on a field call its C<valid> method
$field = $self->field('user.name');
$field->is_required;
$field->valid;
$field->error;
This will only validate and return the error for the C<user.name> field. To validate all fields and retrieve all error messages call the controller's C<valid> and C<errors> methods
$self->field('user.name')->is_required;
$self->field('user.age')->is_like(qr/^\d+$/);
$self->valid;
my $errors = $self->errors;
$errors->{'user.name'}
# ...
Of course the C<error>/C<errors> and C<valid> methods can be used in your view too
<h3>Address #<%= $addr->index + 1 %></h3>
<%= $addr->hidden('id') %>
...
</div>
<% } %></pre>
<h1><a class='u'
name="VALIDATING_&_FILTERING"
>VALIDATING & FILTERING</a></h1>
<p>Validation rules are created by calling validation and/or filter methods on the field to be validated</p>
<pre> # In your controller
my $self = shift;
$self->field('user.name')->is_required;
$self->field('user.name')->filter('trim');</pre>
<p>These methods can be chained</p>
<pre> $self->field('user.name')->is_required->filter('trim');</pre>
<p>To perform validation on a field call its <code>valid</code> method</p>
<pre> $field = $self->field('user.name');
$field->is_required;
$field->valid;
$field->error;</pre>
<p>This will only validate and return the error for the <code>user.name</code> field. To validate all fields and retrieve all error messages call the controller's <code>valid</code> and <code>errors</code> methods</p>
<pre> $self->field('user.name')->is_required;
$self->field('user.age')->is_like(qr/^\d+$/);
$self->valid;
my $errors = $self->errors;
$errors->{'user.name'}
# ...</pre>
<p>Of course the <code>error</code>/<code>errors</code> and <code>valid</code> methods can be used in your view too</p>
( run in 1.111 second using v1.01-cache-2.11-cpan-39bf76dae61 )