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
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
public
|
|
public
boolean
|
|
public
array
|
|
public
string
|
|
public
array
|
public
string
|
$id
The ID of the form |
|
public
string
|
$class
Class to apply to the field |
|
public
string
|
$action
The action of the form |
|
public
string
|
$method
The method of the form |
|
public
boolean
|
$validate
Should the form be validated? |
#
true
|
private
array
|
$fieldsets
An array of fieldsets |