PtcForms User Guide
Html Dynamic Forms Generator/Validator - VERSION 0.8.3
Table of Contents
INTRODUCTIONThe dynamic html forms generator is an excellent tool that will help you build all kinds of html forms in just a few minutes.
It will make you forget about all the problems involved with fields alignment, layouts, and server-side validation.
For client-side validation the wonderfull jquery validator plugin can be used.
This class is especially adviced to webdesigners and webmasters that are having troubles with html forms.
Main Features:
- Fast and easy to use, for building and validating html forms
- Completely flexible, can adapt to any web environment
- Built in validator which supports custom methods
- Automatic labels and fields alignment
- Html templates can be all manipulated
- No additional css files are needed for the class to work
- Uses PtcDebug class to debug the building process
GETTING STARTEDAdd the following lines to load the class:
// initialize the class
require_once('PtcForms.php');
CLASS OPTIONSThis is the full list of options available on intialization, stored in $_defaultOptions property:
$options=array
(
'form_method' => 'post', // the form method to use
'form_action' => '#', // the form action url
'form_width' => '500px', // the width for the main container
'add_class_validator' => false, // add validator classes to fields for use with jquery
'labels_align' => 'left', // align labels globally(left,top,right)
'labels_width' => '40%', // the width for labels as a percentage
'style_elements' => true, // add default style to input elements to align properly
'style_labels' => true, // add default style to label elements to align properly
'style_tables' => true, // add default style to table elements to align properly
'spacer_height' => '3px', // height for the spacer between fields
'keep_values' => true, // repopulate filled fields on form submission
'print_form' => true, // print form to screen or return html only
'start_tab' => "\t", // format html code with tabs
'err_msg_level' => E_USER_WARNING, // error messages level
'debug_category' => 'PtcForms' // default category for the PtcDebug class
);
// initialize the class
require_once('PtcForms.php');
form_methodUse this option to set form method(post/get).
Default value: post (string)
form_actionThis option can be used to set form action attribute. Ex: process-form.php
Default value: # (string)
form_widthUse this option to set the width of the form.
Default value: 500px (string)
add_class_validatorThis option adds the respective css classes to fields to validate client side with jquery and the ptcforms-validator.js plugin. See ADDING AND VALIDATING FIELDS WITH THE VALIDATOR ENGINE for details and usage.
Default value: false (bool)
labels_alignThis option can be used for the form layout, it will attempt to align the labels with the fields. Options are "left",
"right","top" or "none" for no labels.
Default value: left (string)
labels_widthUse "labels_width" to set the width for the labels. Always make sure that the form has the currect width, otherwise the result will not be as expected.
You can refer to form_width for the form width option.
Default value: 40% (string)
style_elementsThis option can be used to stop adding styles to the input fields automatically, quite usefull if you want to use your own css.
The default styles for the inputs are stored in the $_inputStyles property, and can be manipulated with the setInputStyle() method.
see Changing default styles for fields for a detailed usage.
Default value: true (bool)
// default input styles
$_inputStyles=array
(
'radio' => array('padding'=>'0px','margin'=>'0px','vertical-align'=>'middle','width'=>'14px'),
'input' => array('margin'=>'0px','padding'=>'3px','border'=>'1px inset'),//padding:2px;
'button' => array('margin'=>'0px')
);
style_labelsThis option can be used to stop adding styles to the label containers automatically, quite usefull if you want to use your own css.
The default styles for the labels are stored in the $_labelStyles property, and can be manipulated with the setLabelStyle() method.
see Changing default styles for label containers for a detailed usage.
Default value: true (bool)
// default label container styles
$_labelStyles=array
(
/* Styles for input, select and textarea */
1 => array('left'=>array('float'=>'left','margin'=>'2px 0 0 0','width'=>'{label_width}%'),//margin:1px 0 0 0
'right'=>array('float'=>'left','margin'=>'1px 3px 0 0',//margin:0 3px 0 0;
'text-align'=>'right','width'=>'{label_width}%'),
'top'=>array()),
/* Styles for checkbox and radio buttons */
2 => array('left'=>array('vertical-align'=>'middle','border'=>'none'),
'right'=>array('vertical-align'=>'middle','border'=>'none'),
'top'=>array()),
/* Styles for radio/checkbox group and composite fields */
3 => array('left'=>array('float'=>'left','margin'=>'3px 0 0 0','width'=>'{label_width}%'),
'right'=>array('float'=>'left','margin'=>'1px 3px 0 0',
'text-align'=>'right','width'=>'{label_width}%'),
'top'=>array())
);
style_tablesThis option can be used to stop adding width to the tables automatically, quite usefull if you want to use your own css.
Default value: true (bool)
spacer_heightThe "spacer_height" option property can be used to change the height between fields.
Default value: 3px (bool)
keep_valuesThis options is used to load already inserted values in the fields if form was submitted. It could be usefull set to false, for a login form as inserted values will not be reproposed upon submission, as shown in the pctforms-ex1.php example file
Default value: true (bool)
print_formPrints the form or returns the output generated by the render() method as a string if set to false.
Default value: true (bool)
start_tabThis option controls the start tab for the html output. If you would lneed to increase the offset add more tabs, ex: \t\t\t
Default value: \t (string)
err_msg_levelThe "err_msg_level" is used to set the severity of the error for execution errors within the class itself. Use E_USER_NOTICE for a lower severity and E_USER_ERROR for a higher severity
Default value: E_USER_WARNING (const)
default_categoryThe "default_category" is used for the PtcDebug class to set a category for the messages panel
Default value: PtcForms (string)
FIELD PARAMETERSThe following paragraph will cover all possible parameters that can be passed to the addElement() method, and their usage.
// addding a text field
(
'name' => 'fieldName', // mandatory, will throw a warning if not present
'type' => 'text', // text is default if "type" is not present
'label' => 'LabelText' // the text for the label
));
Here is a list of all possibile parameters that can be passed to the addElement() method as array keys for the "$params" parameter:
- name(mandatory)
- type
- label
- attributes
- labelOptions
- parentEl
- events
- validate
- values
- value
Some of these parameters can be added to field values like select, radio and checkbox groups, with "[brackets]".
Ex: attributes[]. See USING [BRACKETS] TO ADD PARAMETERS TO FIELD VALUES for detailed usage.
The following paragraphs, will explain in details the usage of these parameters.
nameThis parameter defines the field name and is mandatory, the class will throw a warning if not set, and the field will not be processed by the compiler.
Type: string
typeThis parameter defines the field type. If not set, "text" will be used.
Possible field types:
- text (default)
- password
- radio
- checkbox
- checkboxgroup
- radiogroup
- textarea
- select
- fieldset
- composite (for complex field layouts)
- custom (any html code can be passed as value)
- submit
If a different "type" is used, the class will attempt to load the default options of the "text" input.
Type: string
labelUse this parameter to set the the text for the label of the field.
Type: string
attributesAdds html attributes to the field, all attributes are stored in _elAttributes property. New attributes can be added with the addElAttribute() and
addElAttributes() methods, see Extending the compiler with custom attributes.
See Adding attributes to fields for examples and detailed usage.
Type: array
labelOptionsAdds html attributes to the label container, all attributes are stored in _elAttributes property. New attributes can be added with the addElAttribute() and
addElAttributes() methods, see Extending the compiler with custom attributes.
See Adding attributes to label container for examples and detailed usage.
Type: array
parentElAdds html attributes to the parent container of the field, all attributes are stored in _elAttributes property. New attributes can be added with the
addElAttribute() and addElAttributes() methods, see Extending the compiler with custom attributes.
See Adding attributes to field container for examples and detailed usage.
Type: array
eventsThis parameter can be used to add javascipt events to the fields.
the following field types are supported:
- text
- password
- textarea
- submit
- select
- radio
- checkbox
- checkboxgroup (will be added to all values)
- radiogroup (will be added to all values)
See ADDING FIELD EVENTS for detailed usage.
Type: array
validateUse this parameter to add the field to the validator engine.
These are the possible validator options:
- required - checks if field has a value
- email - checks if email is a valid format
- equalTo - checks if value is equal to another field specified in the value parameter
- number - checks if the value is numeric
- pattern - checks if the value matches a given regex pattern
Any other value will be interpreted as a custom function.
the following field types are supported:
- text
- password
- textarea
- submit
- select
- radio
- checkbox
- checkboxgroup (will be added to all values)
- radiogroup (will be added to all values)
see Adding Fields To The Validator for example usage.
Type: array|string
valuesThe parameter "values" is used to add values for the following fields:
- select
- radiogroup
- checkboxgroup
- composite
- fieldset
See Adding values as an array (select,radiogroup,checkboxgroup) for examples to add values for select,radiogroup and checkboxgroup.
See Adding fields as an array of values (composite,fieldset) for examples to add values for composite and fieldset.
Type: array
valueThe parameter "value" can be used to add the value as a string for the following fields:
- text
- radio
- checkbox
- textarea
- password
- submit
- custom (any html code can be passed as value)
See Adding values as a string for example usage.
Type: string
ADDING FIELDS EXAMPLESThe addElement() method is used to add form fields. Works with all input types, but some extra parameters can be passed aswell to create custom html.
The "$params" paramater is used to pass the configuration for the field to the method as an array. See FIELD PARAMETERS for all possibile parameters.
The following sections will try to demonstrate how we can add some of these fields.
Adding a Text Field// addding a text field
(
'name' => 'fieldName', // mandatory, will throw a warning if not present
'type' => 'text', // text is default if "type" is not present
'label' => 'LabelText' // the text for the label
));
The following fields can be created like shown in the example above as they do not require a "value":
- text
- radio
- checkbox
- textarea
- password
- submit
The following fields require a "value", or the class will throw a warning:
- select
- radiogroup
- checkboxgroup
- composite
- fieldset
- custom
Adding a Select Field// adding a select field
$values=array(''=>'Choose','enquiry'=>'Enquiry','information'=>'Information',
'billing'=>'Billing','other'=>'Other');
(
'name' => 'fieldName1',
'type' => 'select',
'label' => 'LabelText',
'values' => $values // adding previously created array with values
));
To make an option selected you can use "[brackets]", see USING [BRACKETS] TO ADD PARAMETERS TO FIELD VALUES.
Adding a Checkboxgroup// adding a checkboxgroup
(
'type' => 'checkboxgroup',
'name' => 'fieldName5',
'values' => array('yes'=>'Yes !!! (please)','no'=>'No (thank you)'),
'attributes' => array('cols'=>2) // display values inline
));
the "cols" attribute controls how many columns the group will have. The default value is 1.
To make a checkbox checked you can use "[brackets]", see USING [BRACKETS] TO ADD PARAMETERS TO FIELD VALUES.
Adding a CompositeComposite fields are used for complex layouts, as the can be nested inside each other.
This allows to have many fields inline for example, or a form with fields in 2 columns.
// adding a composite field for previously created fields
(
'name' => 'fieldName2',
'type' => 'composite',
'values' => array('fieldName','fieldName1') // previously created field names
));
The following fields can be added like shown in the example above: fieldset, composite.
the "cols" attribute controls how many columns the group will have. The default value is "inline".
Adding a Custom FieldThis is how we can add a custom field, in this case we will use the addSpacer() method, but keep in mind that any html can be passed as "value" for this field type.
// adding a spacer
(
'type' => 'custom',
'name' => 'spacer1',
));
The custom field type will only accept the following parameters:
- name
- type
- value
Any other parameter will be dropped silently.
ADDING HTML ATTRIBUTES TO ELEMENTSThis section will try to cover how to add attributes to fields, labels and containers.
All Attributes are stored in _elAttributes property. New attributes can be added with the addElAttribute() and addElAttributes() methods,
see Extending the compiler with custom attributes.
// these are the default elements attributes, more can be added with the addElAttributes() method
$_elAttributes=array('class','id','style','value','maxlength','size','disabled','checked','target','align','events',
'title','selected','cols','rows','equalTo','border','pattern','cellpadding','cellspacing');
Adding attributes to fieldsThe parameter "attributes" is used to add html attributes to form fields.
// adding a textarea field
(
'type' => 'textarea',
'name' => 'fieldName8',
'label' => 'labelText',
// adding html attributes to the textarea, set style=>false to remove default styles for 1 field
'attributes' => array('id'=>'fieldID','class'=>'some-class','style'=>false)
));
To add attributes to values for select and raidio/checkbox groups we can use "[brackets]" like shown in the next example:
// adding a select field
$values=array(''=>'Choose','enquiry'=>'Enquiry','information'=>'Information',
'billing'=>'Billing','other'=>'Other');
(
'name' => 'fieldName10',
'type' => 'select',
'label' => 'LabelText',
'values' => $values,
'attributes[]' => array('class'=>'someClass'), // will add the class to all select options
'attributes[enquiry]' => array('selected'=>1) // making 1 option selected
));
See USING [BRACKETS] TO ADD PARAMETERS TO FIELD VALUES for more details.
Adding attributes to label containerThe parameter "labelOptions" is used to add html attributes to the label container.
// adding a textarea field
(
'type' => 'textarea',
'name' => 'fieldName11',
'label' => 'labelText',
// setting a different style and changing alignment for a label container
'labelOptions' => array('align'=>'top','style'=>'margin:10px')
));
To add attributes to label containers of the field values for radio/checkbox groups we can use "[brackets]" like shown in the next example:
// adding a radiogroup
(
'type' => 'radiogroup',
'name' => 'fieldName12',
'values' => array('yes'=>'Yes !!! (please)','no'=>'No (thank you)'),
'labelOptions' => array('style'=>false), // removing default style for label container of the field
'labelOptions[]' => array('align'=>'top'), // aligning labels of all values to the top
'labelOptions[yes]' => array('class'=>'some-class') // adding a class to the label container of 1 value
));
See USING [BRACKETS] TO ADD PARAMETERS TO FIELD VALUES for more details.
Adding attributes to field containerThe parameter "parentEl" is used to add html attributes to the field container.
// adding a submit button
(
'type' => 'submit',
'name' => 'fieldName13',
'value' => 'Submit', // adding a value to the field
'parentEl' => array('style'=>'text-align:right;') // adding style to the parent container
));
To add attributes to the parent containers of the field values for radio/checkbox groups we can use "[brackets]" like shown in the next example:
// adding a checkboxgroup
(
'type' => 'checkboxgroup',
'name' => 'fieldName14',
'values' => array('yes'=>'Yes !!! (please)','no'=>'No (thank you)'),
'parentEl[]' => array('class'=>'some-class'), // adding a class to the parent container of all values
'parentEl[yes]' => array('id'=>'someID') // adding an id to the parent container of 1 value
));
See USING [BRACKETS] TO ADD PARAMETERS TO FIELD VALUES for more details.
Extending the compiler with custom attributesThe addElAttribute() and addElAttributes() methods are used to add more attributes to be compiled with the fields and containers.
The default attributes are stored in the _elAttributes property.
// adding an attribute to the the _elAttributes property
$form->AddElAttributes('default-value');
// adding an array of attributes
$form->AddElAttributes(array('default-value','tabindex'));
CHANGING DEFAULT STYLES APPLIED TO ELEMENTSThe methods setInputStyle() and setLabelStyle() can be used to manipulate the default styles for fields and label containers.
The following chapters will try to cover these methods with practical examples.
Changing default styles for fieldsThe setInputStyle() method is used to change the default styles for the fields.
// changing the default styles for radio/checkbox fields, removing "vertical-align" property
$form->setInputStyle(array('margin'=> '0 10px 0 0','vertical-align'=> false),'radio');
// changing the default styles for input fields
$form->setInputStyle(array('padding'=> '3px','border'=> 'none'),'input');
The default styles are stored in the $_inputStyles property.
// default input styles
$_inputStyles=array
(
'radio' => array('padding'=>'0px','margin'=>'0px','vertical-align'=>'middle','width'=>'14px'),
'input' => array('margin'=>'0px','padding'=>'3px','border'=>'1px inset'),//padding:2px;
'button' => array('margin'=>'0px')
);
Changing default styles for label containersThe setLabelStyle() method is used to change the default styles for the label containers.
The "$type" parameter is to control the alignment of the fields, if left empty,
the default "labels_align" option will be used.
// changing the default styles for the label container for input,select and textarea
// changing the default styles for the label container for radios and checkboxes
$form->setLabelStyle(array('padding'=> '5px','vertical-align'=> false),2);
// adding the $type parameter to change the default style for a radiogroup with labels aligned right
$form->setLabelStyle(array('padding'=> '3px','border'=> 'none'),3,'right');
The default styles are stored in the $_labelStyles property.
// default label container styles
$_labelStyles=array
(
/* Styles for input, select and textarea */
1 => array('left'=>array('float'=>'left','margin'=>'2px 0 0 0','width'=>'{label_width}%'),//margin:1px 0 0 0
'right'=>array('float'=>'left','margin'=>'1px 3px 0 0',//margin:0 3px 0 0;
'text-align'=>'right','width'=>'{label_width}%'),
'top'=>array()),
/* Styles for checkbox and radio buttons */
2 => array('left'=>array('vertical-align'=>'middle','border'=>'none'),
'right'=>array('vertical-align'=>'middle','border'=>'none'),
'top'=>array()),
/* Styles for radio/checkbox group and composite fields */
3 => array('left'=>array('float'=>'left','margin'=>'3px 0 0 0','width'=>'{label_width}%'),
'right'=>array('float'=>'left','margin'=>'1px 3px 0 0',
'text-align'=>'right','width'=>'{label_width}%'),
'top'=>array())
);
MANIPULATING HTML TEMPLATESThe customTpl() and customTpls() methods can be used to manipulate the default html templates stored in the _htmlTpls property.
The following examples show how we can add an id to the main container holding the form, and replacing the spacer template to add "hr".
// changing the default template for the form container to add an id
$id='someID';
$form->customTpl(array('main_container'=> '<div style="width:{form_width}" id='. $id. '>{form}</div>'));
// replacing the main spacer template to add "<hr>" instead of "<!-- -->"
$width="90%";
$form->customTpl(array('spacer'=> '<div style="clear:both;height:{spacerVal}" {id}><hr style="width:'. $width. ';"></div>'));
The default templates for all html elements are stored in the $_htmlTpls property.
// html templates for all elements used by PtcForms component
$_htmlTpls=array
(
'select_option' => '<option {attributes}>{label}</option>',
'select' => '<select name="{name}" {attributes}>{options}</select>',
'textarea' => '<textarea name="{name}" {attributes}>{value}</textarea>',
'input' => '<input type="{type}" name="{name}" {attributes}>',
'fieldset' => '<fieldset {attributes}>{label}{data} {start_tab}</fieldset>',
'form' => '<form {method} {action} {attributes}>{fields}</form>',
'spacer' => '<div style="clear:both;height:{spacerVal}" {id}><!-- --></div>',
'label' => '<label {for} {attributes}>{label}</label>',
'legend' => '<legend {attributes}>{label}</legend>',
'span' => '<span {attributes}>{label}</span>',
'td' => '<td align="left" valign="top">',
'table' => '<table {attributes}>',
'field_container' => '<div {attributes}>{label}{field} {start_tab}</div>',
'main_container' => '<div style="width:{form_width}">{form}</div>'
);
ADDING VALUES TO FIELDSThe parameters "values" and "value" are used to add values to form fields.
An array is passed for select,checkboxgroup,radiogroup,fieldset and composite fields.
The rest of the fields use a string as value.
Adding values as a stringThe parameter "value" can be used to add a value as a string for the following fields:
- text
- radio
- checkbox
- textarea
- password
- submit
- custom (any html code can be passed as value)
This is how we can add a value to a field as a string:
// adding a submit button
(
'type' => 'submit',
'name' => 'fieldName3',
'value' => 'Submit' // adding a value to the field
));
The same result can be achieved with the "attributes" parameter.
// adding a submit button
(
'type' => 'submit',
'name' => 'fieldName4',
'attributes' => array('value'=>'Submit') // same as the "value" parameter
));
Adding values as an array (select,radiogroup,checkboxgroup)The parameter "values" can be used to add an array of values for the following fields:
- select
- radiogroup
- checkboxgroup
The following examples demonstrate how values can be added as an array, with values as array keys and labels as array values:
// adding a select field
$values=array(''=>'Choose','enquiry'=>'Enquiry','information'=>'Information',
'billing'=>'Billing','other'=>'Other');
(
'name' => 'fieldName1',
'type' => 'select',
'label' => 'LabelText',
'values' => $values // adding previously created array with values
));
To add parameters to values "[brackets]" can be used, see USING [BRACKETS] TO ADD PARAMETERS TO FIELD VALUES for more details.
Adding fields as an array of values (composite,fieldset)The parameter "values" can be used to add an array
with previously created field names as values for the following fields:
- fieldset
- composite
The following examples demostrate how values can be added as an array with the names of the previously created fields as values.
// adding a composite field for previously created fields
(
'name' => 'fieldName2',
'type' => 'composite',
'values' => array('fieldName','fieldName1') // previously created field names
));
Composite fields can be nested to achieve complex layouts, but rememeber to always add the fields in the proper order or the class will not find them.
USING [BRACKETS] TO ADD PARAMETERS TO FIELD VALUESThe following fields allow the use of "[brackets]" to add parameters to the values.
- select
- radiogroup
- checkboxgroup
the select input only accepts brackets for the "attributes" parameter.
// adding a select field
$values=array(''=>'Choose','enquiry'=>'Enquiry','information'=>'Information',
'billing'=>'Billing','other'=>'Other');
(
'name' => 'fieldName6',
'type' => 'select',
'label' => 'LabelText',
'values' => $values,
'attributes[]' => array('class'=>'someClass') // will add the class to all select options
));
Radio/checkbox groups accept the following parameters with "[brackets]":
- attributes
- labelOptions
- parentEl
- events (if field name is specified)
- validate (if field name is specified)
To interact with all values just use []. To interact with only 1 value use the array key of the respective value like shown in the next example:
// adding a radiogroup
(
'type' => 'radiogroup',
'name' => 'fieldName7',
'values' => array('yes'=>'Yes !!! (please)','no'=>'No (thank you)'),
'labelOptions[]' => array('align'=>'top'), // aligning labels of all values to the top
'attributes[yes]' => array('checked'=>1), // making 1 value checked
));
The only 2 exceptions are "events" and "validate"
which will be added to all values also without "[brackets]", as no validation or events can be attached to the main field.
ADDING FIELD EVENTSThe "events" parameter is used to attach javascript events to form fields.
// adding a submit button
(
'type' => 'submit',
'name' => 'fieldName15',
'events' => array('onClick'=>'return false;') // attaching js events to the field
));
radio/checkboxgroup will add js events to all field values.
To add js events to 1 value for radio/checkboxgroup use "[brackets]", as shown in the next example:
// adding a checkboxgroup
(
'type' => 'checkboxgroup',
'name' => 'fieldName16',
'values' => array('yes'=>'Yes !!! (please)','no'=>'No (thank you)'),
'events[yes]' => array('onClick'=>'return false;') // attaching js events to 1 field value
));
See USING [BRACKETS] TO ADD PARAMETERS TO FIELD VALUES for more details.
RENDERING THE FORMThe render() method is used to build and output the form.
The method accepts 2 argumets, "$attributes" and "$events" for the form.
// building and printing the form
// passing some attributes and events to the form
$attributes=array('id'=>'formID','class'=>'some-class');
$events=array('onSubmit'=>'return false;');
$form->render($attributes,$events);
To only build the form and store the html inside a variable, we can set the print_form option to false.
// initialize the class, but don't print the form
require_once('PtcForms.php');
$form= new PtcForms(array("print_form"=> false));
// storing the form inside a $var with "print_form" option set to false
ADDING AND VALIDATING FIELDS WITH THE VALIDATOR ENGINEThe following section will try to cover the validation process (server-side) of a form, by using the class built-in engine.
For validating client-side, you can use the ptcforms-validator.js ui-plugin.
Adding Fields To The ValidatorThe validate property adds the form fields to the validator engine.
If add_class_validator option is set to true, validator will add the above options as a class attribute to the fields for jquery validator plugin
ptcforms-validator.js. "pattern" and "equalTo" will be added as attributes, as they are in the _elAttributes property..
These are the possible validator options:
- required - checks if field has a value
- email - checks if email is a valid format
- equalTo - checks if value is equal to another field specified in the value parameter
- number - checks if the value is numeric
- pattern - checks if the value matches a given regex pattern
// addding a password field
(
'name' => 'passwdField',
'type' => 'password',
'label' => 'LabelText',
'validate' => 'required' // adding required for this field
));
// addding 1 more password field
(
'name' => 'passwdField1',
'type' => 'password',
'label' => 'LabelText',
'validate' => array('required','equalTo'=>'passwdField') // adding multiple validators for this field
));
Any other value will be interpreted as a custom function.
// some custom function that will be added to the validator(taken from ptcforms-ex1.php)
function some_func()
{
// do something with $_POST or $_GET arrays and return true or false
}
// adding a text field
(
'name' => 'fieldName17',
'type' => 'text',
'label' => 'LabelText',
'validate' => 'some_func'
));
Radio/checkbox groups will add validation to all field values, to add validation to 1 value for radio/checkboxgroup use "[brackets]":
// adding a checkboxgroup
(
'type' => 'checkboxgroup',
'name' => 'fieldName16',
'values' => array('yes'=>'Yes !!! (please)','no'=>'No (thank you)'),
'validate[yes]' => 'required' // adding validation to 1 checkbox only
));
See USING [BRACKETS] TO ADD PARAMETERS TO FIELD VALUES for more details.
Validating The Form (Server Side)The validate() method validates the form fields defined with the "validate" parameter,
and returns an array with isValid(bool) and errors(array) as array keys.
// adding a submit button
(
'name' => 'lg_login',
'type' => 'submit',
'value' => 'Login'
));
$err_msg='';
if(@$_POST['lg_login']) // check if the form was sent
{
/* VALIDATE THE FORM SERVER SIDE */
if(!$validate['isValid']) // returns a bool(true/false)
{
$err_msg='<div class="errMsg" style="text-align:center;width:'.$options['form_width'].'">
Wrong user ID or password!</div><br>';
}
}
For the validator to work, remember to always call the validate() method after building the fields, otherwise the validator will not be aware of the form fields.
To get around this problem, you can try to run the following methods against the $_POST or $_GET arrays:
- validateEmail()
- validateEqualTo()
- validateNumber()
- validatePattern()
- validateRequired()
This example will try to demonstrate the possibility of using these methods separately:
$err_msg='';
if(@$_POST['lg_login']) // check if the form was sent
{
if(!$form->validateNumber('someFieldName',$_POST)) // check if value is numeric
{
// form is not valid
}
else if(!$form->validateEmail('otherFieldName',$_POST)) // check if value is an email
{
// form is not valid
}
else
{
// form is valid
}
}
Prev |
|
Next |
PtcDb User Guide |
|
|
|
|