NAME

Mojolicious::Plugin::FormFieldsFromJSON - create form fields based on a definition in a JSON file

VERSION

version 0.01

SYNOPSIS

# Mojolicious
$self->plugin('FormFieldsFromJSON');

# Mojolicious::Lite
plugin 'FormFieldsFromJSON';

DESCRIPTION

Mojolicious::Plugin::FormFieldsFromJSON is a Mojolicious plugin.

HELPER

form_fields

$controller->form_fields( 'formname' );

FIELD DEFINITIONS

This plugin supports several form fields:

  • text

  • checkbox

  • radio

  • select

Those fields have the following definition items in common:

  • label

  • type

  • data

  • data_function

  • attributes

    Attributes of the field like "class"

EXAMPLES

The following sections should give you an idea what's possible with this plugin

text

A simple text field

Set CSS classes

select

Simple: Value = Label

When you have a list of values for a select field, you can define an array reference:

[
  {
    "type" : "select",
    "name" : "language",
    "data" : [
      "de",
      "en"
    ]
  }
]

This creates the following select field:

<select id="language" name="language">
    <option value="de">de</option>
    <option value="en">en</option>
</select>

Preselect a value

You can define

[
  {
    "type" : "select",
    "name" : "language",
    "data" : [
      "de",
      "en"
    ],
    "selected" : "en"
  }
]

This creates the following select field:

<select id="language" name="language">
    <option value="de">de</option>
    <option value="en" selected="selected">en</option>
</select>

If a key named as the select exists in the stash, those values are preselected (this overrides the value defined in the .json):

$c->stash( language => 'en' );

and

[
  {
    "type" : "select",
    "name" : "language",
    "data" : [
      "de",
      "en"
    ]
  }
]

This creates the following select field:

<select id="language" name="language">
    <option value="de">de</option>
    <option value="en" selected="selected">en</option>
</select>

Multiselect

[
  {
    "type" : "select",
    "name" : "languages",
    "data" : [
      "de",
      "en",
      "cn",
      "jp"
    ],
    "multiple" : 1,
    "size" : 3
  }
]

This creates the following select field:

<select id="languages" name="languages" multiple="multiple" size="3">
    <option value="cn">cn</option>
    <option value="de">de</option>
    <option value="en">en</option>
    <option value="jp">jp</option>
</select>

Preselect multiple values

[
  {
    "type" : "select",
    "name" : "languages",
    "data" : [
      "de",
      "en",
      "cn",
      "jp"
    ],
    "multiple" : 1,
    "selected" : [ "en", "de" ]
  }
]

This creates the following select field:

<select id="language" name="language">
    <option value="cn">cn</option>
    <option value="de" selected="selected">de</option>
    <option value="en" selected="selected">en</option>
    <option value="jp">jp</option>
</select>

Values != Label

[
  {
    "type" : "select",
    "name" : "language",
    "data" : {
      "de" : "German",
      "en" : "English"
    }
  }
]

This creates the following select field:

<select id="language" name="language">
    <option value="en">English</option>
    <option value="de">German</option>
</select>

Option groups

[
  {
    "type" : "select",
    "name" : "language",
    "data" : {
      "EU" : {
        "de" : "German",
        "en" : "English"
      },
      "Asia" : {
        "cn" : "Chinese",
        "jp" : "Japanese"
      }
    }
  }
]

This creates the following select field:

<select id="language" name="language">
    <option value="en">English</option>
    <option value="de">German</option>
</select>

Disable values

[
  {
    "type" : "select",
    "name" : "languages",
    "data" : [
      "de",
      "en",
      "cn",
      "jp"
    ],
    "multiple" : 1,
    "disabled" : [ "en", "de" ]
  }
]

This creates the following select field:

<select id="language" name="language">
    <option value="cn">cn</option>
    <option value="de" disabled="disabled">de</option>
    <option value="en" disabled="disabled">en</option>
    <option value="jp">jp</option>
</select>

radio

checkbox

SEE ALSO

Mojolicious, Mojolicious::Guides, http://mojolicio.us.

AUTHOR

Renee Baecker <reneeb@cpan.org>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2014 by Renee Baecker.

This is free software, licensed under:

The Artistic License 2.0 (GPL Compatible)