Mojolicious-Plugin-FormFieldsFromJSON

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

        };

      Now you can use

        [
          {
            "label" : "Name",
            "type" : "testfield",
            "name" : "name"
          }
        ]

      For more details see Additional Types.

HELPER

 form_fields

    form_fields returns a string with all configured fields "translated" to
    HTML.

      $controller->form_fields( 'formname' );

    Given this configuration:

     [
        {
            "label" : "Name",
            "type" : "text",
            "name" : "name"
        },
        {
            "label" : "City",
            "type" : "text",
            "name" : "city"
        }
     ]

    You'll get

     <input id="name" name="name" type="text" value="" />
     <input id="city" name="city" type="text" value="" />

  dynamic config

    Instead of a formname, you can pass a config:

      $controller->form_fields(
        [
          {
            "label" : "Name",
            "type" : "testfield",
            "name" : "name"
          }
        ]
      );

    This way, you can build your forms dynamically (e.g. based on database
    entries).

 validate_form_fields

    This helper validates the input. It uses the
    Mojolicious::Validator::Validation and it validates all fields defined
    in the configuration file.

    For more details see Validation.

 forms

    This method returns a list of forms. That means the filenames of all
    .json files in the configured directory.

      my @forms = $controller->forms;

    The filenames are returned without the file suffix .json.

 fields

    fields() returns a list of fields (label or name).

      my @fieldnames = $controller->fields('formname');

    If your configuration looks like

     [
       {
         "label" : "Email",
         "name"  : "email",
         "type"  : "text"
       },
       {
         "name"  : "password",
         "type"  : "password"
       }
     ]

    You get

      (
        Email,
        password
      )

FIELD DEFINITIONS

    This plugin supports several form fields:

      * text

      * checkbox

      * radio

      * select

      * textarea

      * password

      * hidden

    Those fields have the following definition items in common:

README  view on Meta::CPAN


      <label for="name">Name:</label><div><input id="name" name="name" type="text" value="" /></div>
      
       
      Country: <select id="country" name="country"><option value="au">au</option></select>

 A field specific template

    When you want to use a different template for a specific field, you can
    use the template field in the configuration file.

      plugin 'FormFieldsFromJSON' => {
        dir       => File::Spec->catdir( dirname( __FILE__ ) || '.', 'conf' ),
        template  => '<label for="<%= $id %>"><%= $label %>:</label><div><%= $field %></div>',
      };

    With a configuration file like

     [
        {
            "label" : "Name",
            "type" : "text",
            "name" : "name"
        }
        {
            "label" : "Country",
            "type" : "select",
            "name" : "country",
            "data" : [ "au" ],
            "template" : "<%= $label %>: <%= $field %>"
        }
     ]

    You get

      <label for="name">Name:</label><div><input id="name" name="name" type="text" value="" /></div>
      
       
      Country: <select id="country" name="country"><option value="au">au</option></select>

 Template variables

    You get three template variables for free:

      * $label

      If a label is defined in the field configuration

      * $field

      The form field (HTML)

      * $id

      The id for the field. If no id is defined, the name of the field is
      set.

Validation

    You can define some validation rules in your config file. And when you
    call validate_form_fields, the fields defined in the configuration file
    are validated.

    Mojolicious::Validator::Validation is shipped with some basic
    validation checks:

      * in

      * size

      * like

      * equal_to

    There is Mojolicious::Plugin::AdditionalValidationChecks with some more
    basic checks. And you can also define your own checks.

    The validation field is a hashref where the name of the check is the
    key and the parameters for the check can be defined in the value:

      "validation" : {
          "size" : [ 2, 5 ]
      },

    This will call ->size(2,5). If you want to pass a single parameter, you
    can set a scalar:

      "validation" : {
          "equal_to" : "foo"
      },

    Validation checks are done in asciibetical order.

    You can also use the filters:

      "validation" : {
          "size" : [ 2, 5 ],
          "filters" : [ "trim" ]
      },

 Check a string for its length

    This is a simple check for the length of a string

     [
        {
            "label" : "Name",
            "type" : "text",
            "validation" : {
                "size" : [ 2, 5 ]
            },
            "name" : "name"
        }
     ]

    Then you can call validate_form_fields:

      my %errors = $c->validate_form_fields( $config_name );

    In the returned hash, you get the fieldnames as keys where a validation
    check fails.

 A mandatory string

    If you have mandatory fields, you can define them as required

     [
        {
            "label" : "Name",
            "type" : "text",
            "validation" : {
                "required" : "name"
            },
            "name" : "name"
        }
     ]

 Provide your own error message

    With the simple configuration seen above, the %error hash contains the
    value "1" for each invalid field. If you want to get a better error
    message, you can define a hash in the validation config

     [
        {
            "label" : "Name",
            "type" : "text",
            "validation" : {
                "like" : { "args" : [ "es" ], "msg" : "text must contain 'es'" },
                "size" : { "args" : [ 2, 5 ], "msg" : "length must be between 2 and 5 chars" }
            },
            "name" : "name"
        }
     ]

    Examples:

      text   | error
      -------+---------------------------------
      test   |
      t      | text must contain 'es'
      tester | length must be between 2 and 5 chars

Translation

    Most webapplications nowadays are internationalized, therefor this
    module provides some support for translations.

    If translate_labels is set to a true value, a template is used and
    translation_method is given, the labels are translated.

 translation_method

    translation_method has to be a reference to a subroutine.

  An example for translation

    Load and configure the plugin:



( run in 1.219 second using v1.01-cache-2.11-cpan-d8267643d1d )