Mojolicious-Plugin-FormFields
view release on metacpan or search on metacpan
<p>See <a href="http://search.cpan.org/perldoc?Mojolicious%3A%3APlugin%3A%3AParamExpand" class="podlinkpod"
>Mojolicious::Plugin::ParamExpand</a> for more info.</p>
<h2><a class='u'
name="SCOPING"
>SCOPING</a></h2>
<p>Fields can be scoped to a particular object/data structure via the <code><a href="#fields" class="podlinkpod"
>"fields"</a></code> helper</p>
<pre> my $user = fields('user');
$user->text('name');
$user->hidden('id');</pre>
<p>When using <code>fields</code> you must supply the field's name to the HTML input and validation methods, otherwise the calls are the same as they are with <code>field</code>.</p>
<h2><a class='u'
name="COLLECTIONS"
>COLLECTIONS</a></h2>
<p>You can also create fields scoped to elements in a collection</p>
<pre> my $addresses = field('user.addresses');
for my $addr (@$addresses) {
# field('user.addresses.N.id')->hidden
$addr->hidden('id');
# field('user.addresses.N.street')->text
$addr->text('street');
# field('user.addresses.N.city')->select([qw|OAK PHL LAX|])
$addr->select('city', [qw|OAK PHL LAX|]);
}</pre>
<p>Or, for fields that are already scoped</p>
<pre> my $user = fields('user')
$user->hidden('id');
my $addressess = $user->fields('addresses');
for my $addr (@$addresses) {
$addr->hidden('id')
# ...
}</pre>
<p>You can also access the underlying object and its position within a collection via the <code>object</code> and <code>index</code> methods.</p>
<pre> <% for my $addr (@$addresses) { %>
<div id="<%= dom_id($addr->object) %>">
<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>
<pre> <% unless(valid()) { %>
<p>Hey, fix the below errors</p>
<% } %>
<%= field('name')->text %>
<% unless(field('name')->valid) { %>
<span class="error"><%= field('name')->error %></span>
<% } %></pre>
<p>When creating validation rules for <a href="#fields" class="podlinkpod"
>"fields"</a> you must pass the field name as the first argument</p>
<pre> my $user = fields('user');
$user->is_required('password');
$user->is_equal(password => 'confirm_password');
$user->is_long_at_least(password => 8, 'Mais longo caipira');</pre>
<h2><a class='u'
name="AVAILABLE_RULES_&_FILTERS"
>AVAILABLE RULES & FILTERS</a></h2>
<p><code>Mojolicious::Plugin::FormFields</code> uses <code>Validate::Tiny</code>, see <a href="http://search.cpan.org/perldoc?Validate%3A%3ATiny#filter" class="podlinkpod"
>its docs</a> for a list.</p>
<h2><a class='u'
name="RENAMING_THE_VALIDATION_METHODS"
>RENAMING THE VALIDATION METHODS</a></h2>
<p>In the event that the <code>valid</code> and/or <code>errors</code> methods clash with exiting methods/helpers in your app you can rename them by specifying alternate names when loading the plugin</p>
<pre> $self->plugin('FormFields', methods => { valid => 'form_valid', errors => 'form_errors' });
# ...
$self->field('user.name')->is_required;
$self->form_valid;
$self->form_errors;</pre>
<p>Note that this <i>only</i> changes the methods <b>on the controller</b> and does not change the methods on the object returned by <code>field</code>.</p>
<h1><a class='u'
name="METHODS"
>METHODS</a></h1>
<h2><a class='u'
name="field"
>field</a></h2>
<pre> field($name)->text
field($name, $object)->text</pre>
( run in 0.381 second using v1.01-cache-2.11-cpan-39bf76dae61 )