The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Mojolicious::Plugin::NamespaceForm - Support foo.0.bar params

VERSION

0.01

DESCRIPTION

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.

Example

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>

How does it work?

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

SYNOPSIS

Single object

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">

Multiple objects

  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) {
      # ...
    }
  };

Template:

  % stash field_namespace => 'user';
  % stash field_index => 0;
  % for my $user (@$users) {
    %= text_field 'email';
    %= text_field 'name';
    % stash->{field_index}++;
  % }

Output:

  <input name="user.0.email">
  <input name="user.0.name">
  <input name="user.1.email">
  <input name="user.1.name">
  ...

HELPERS

namespace_params

  $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.

See "NAMESPACE OBJECT METHODS" for more details.

METHODS

register

  $self->register(helpers => [qw( input_tag )]);

Will register "HELPERS" and override tag helpers.

NAMESPACE OBJECT METHODS

get

  $hash_ref = $self->get($index);

Return a given hash ref by index, or undef if no such index is defined.

single

  $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.

AUTHOR

Jan Henning Thorsen - jhthorsen@cpan.org