Overview

Namespaces

  • Sleepy
  • Module
    • Authentication
    • CSV
    • DB
    • FormBuilder
    • FSDB
    • IP2Country
    • Mailer
    • MobiDetect
    • Navigation
    • StaticCache
  • PHP

Classes

  • Form
  • States
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo

Class Form

Builds and valids forms

This class allows for building forms using JSON. Fields are automatically validated based on Rules allowing for easy server-side validation. The markup closely resembles jQuery validation plugin so you can use one stylesheet for both client- and server-side validation.

Usage

$UserEdit = new \Module\FormBuilder\Form('{
    "id": "user",
    "action": "#",
    "method": "POST",
    "fieldsets": [
      {
        "legend": "Update your user information:",Fnew
        "fields": [
          {
            "name": "txtName",
            "label": "Name",
            "dataMap": "name",
            "type": "text",
            "value": "Jaime Rodriguez",
            "rules": {
              "required": true,
              "lengthMax": 20
            }
          }, {
            "name": "txtEmail",
            "label": "Email",
            "dataMap": "email",
            "type": "text",
            "value": "hi.i.am.jaime@gmail.com",
            "rules": {
              "required": true,
              "email": true
            }
          }, {
            "name": "txtDate",
            "label": "Date",
            "dataMap": "date",
            "type": "text",
            "value": "04/11/1984",
            "rules": {
              "required": true,
              "date": true
            }
          }, {
            "name": "ddlRole",
            "label": "Role",
            "dataMap": "role",
            "type": "select",
            "values": [
              {
                "name":  "Administrator",
                "value": "admin"
              }, {
                "name":  "Subscriber",
                "value": "subscriber"
              }, {
                "name":  "User",
                "value": "user",
                "selected": true
              }
            ]
          }
        ]
      }, {
        "class": "submit",
        "fields": [
          {
            "name": "btnSubmit",
            "label": "",
            "value": "Submit",
            "type": "submit"
          }
        ]
      }
    ]
  }');

  // Simulate a record Object
  $u = new stdClass();
  $u->columns = array();

  // Has the form been submitted?
  if ($UserEdit->submitted()) {
    // Validate the form
    $passed = $UserEdit->validate();

    if ($passed === true) {
      // put the values into the record Object
      $u->columns = array_merge($u->columns, $UserEdit->getDataMap());
    }
  }

  // if Form::validate() was executed, it will render with errors and
  // updated values, otherwise it'll render normally
  echo $UserEdit->render();

Changelog

Version 1.6

  • Added track attribute for google data tracking

Version 1.5

  • Throws an exception when you make a JSON error

Version 1.4

  • Added ability to overwrite errors
  • Fixed equalTo rule validation bug

Version 1.2

  • Added placeholder for inputs
  • Added namespacing
  • Fixed error class bug

Version 1.1

  • Added the date and changelog sections to documentation
Namespace: Module\FormBuilder
License: http://opensource.org/licenses/MIT
Author: Jaime A. Rodriguez hi.i.am.jaime@gmail.com
Version: 1.6
Date: September 3, 2014
Located at class.formbuilder.php
Methods summary
public
# __construct( string $json )

Creates a Form based on JSON

Creates a Form based on JSON

Parameters

$json
public boolean
# submitted( )

Checks if the form has been submitted?

Checks if the form has been submitted?

Returns

boolean
public array
# validate( )

Validates the form

Validates the form

Returns

array
public string
# render( )

Renders a form

Renders a form

Returns

string
public array
# getDataMap( )

Get the datamap for the form

Get the datamap for the form

Returns

array
Properties summary
public string $id

The ID of the form

The ID of the form

#
public string $class

Class to apply to the field

Class to apply to the field

#
public string $action

The action of the form

The action of the form

#
public string $method

The method of the form

The method of the form

#
public boolean $validate

Should the form be validated?

Should the form be validated?

# true
private array $fieldsets

An array of fieldsets

An array of fieldsets

#
sleepyMUSTACHE v.0.8 API documentation generated by ApiGen