Mojolicious::Plugin::NamespaceForm - Support foo.0.bar params
0.01
This plugin makes it easier to work with multiple forms on a webpages.
This plugins solves the problem related to validation and automatic form filling. That logic is based on the name of the form field, meaning you need to provide unique names for each form of the same type, unless you want confusing error messages displayed to the user.
The forms below is supposed to illustrate the problem:
<form> <h2>New product</h2> Product name: <input name="product_name"> <button>Add</button> <h2>Existing product</h2> Product name: <input name="product_name"> <button name="id" value="42">Update</button> </form>
This plugin works by wrapping around most of the built in form helpers with extra logic for generating the name of the input field. The helpers below is overridden by default, but the list will probably get longer in the future.
check_box hidden_field label_for number_field password_field radio_button select_field text_area text_field url_field
Application/controller logic:
use Mojolicious::Lite; plugin 'Mojolicious::Plugin::NamespaceForm'; post '/user' => sub { my $self = shift; my $user = $self->namespace_params('user')->single; # $user = { email => '...', name => '...', _index => 42 } };
Template:
% stash field_namespace => 'user'; % stash field_index => 42; # optional %= text_field 'email'; %= text_field 'name';
Output:
<input name="user.42.email"> <input name="user.42.name">
post '/users' => sub { my $self = shift; my @users = @{ $self->namespace_params('user') }; # @users = ( # { email => '...', name => '...', _index => 0 }, # { email => '...', name => '...', _index => 1 }, # ); for my $user (@users) { # ... } };
% stash field_namespace => 'user'; % stash field_index => 0; % for my $user (@$users) { %= text_field 'email'; %= text_field 'name'; % stash->{field_index}++; % }
<input name="user.0.email"> <input name="user.0.name"> <input name="user.1.email"> <input name="user.1.name"> ...
$obj = $self->namespace_params($namespace); @list_of_hashes = @$obj; $hash_ref = $obj->single; # might die $hash_ref = $obj->get($index); # might return undef
The $obj is overloaded in list context: It will return a list of hash-refs ordered by _index.
$obj
_index
See "NAMESPACE OBJECT METHODS" for more details.
$self->register(helpers => [qw( input_tag )]);
Will register "HELPERS" and override tag helpers.
$hash_ref = $self->get($index);
Return a given hash ref by index, or undef if no such index is defined.
$hash_ref = $self->single;
This method will die if no data exists for form namespace or if there are more than one item. The index does not matter.
Jan Henning Thorsen - jhthorsen@cpan.org
jhthorsen@cpan.org
To install Mojolicious::Plugin::NamespaceForm, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious::Plugin::NamespaceForm
CPAN shell
perl -MCPAN -e shell install Mojolicious::Plugin::NamespaceForm
For more information on module installation, please visit the detailed CPAN module installation guide.