Documentation 2.x and 3.x

Form generator / validator

ImpressPages CMS has an integrated form generator and validator.

Form widget example plugin (complete code sample to start with).

Generate form

To generate a form create $form = new \Modules\developer\form\Form() object and populate it with required fields using addField method. Complete HTML is generated using $form->render() method.

Simple example to generate a form with one field:

<?php
//create form object
$form = new \Modules\developer\form\Form();
 
//add text field to form object
$field = new \Modules\developer\form\Field\Text(
array(
    'name' => 'firstField', //html "name" attribute
    'label' => 'First field', //field label that will be displayed next to input field
));
$form->addField($field);
 
//print form
return $form->render();
?>

In this way generated form will inherit default theme styles and will save you a lot of time.

Field types

ImpressPages CMS supports following field types:

  • Captcha
  • Confirm (checkbox with text)
  • Email
  • Hidden
  • Password
  • Radio
  • Select
  • Submit
  • Text
  • Textarea
  • File (since 3.0)

To create your own field type just extend Modules\developer\form\Field\Field class.

"Field" appears twice to make class autoloader to work. It's not a mistake.

Validation

Currently ImpressPages CMS has two validation rules that can be applied to fields: 'Required', 'Email'. This is how they can be used:

<?php
//add text field to form object
$field = new \Modules\developer\form\Field\Text(
array(
    'name' => 'firstField', //html "name" attribute
    'label' => 'First field', //field label that will be displayed next to input field
));
$field->addValidator('Required');
$field->addValidator('Email');
$form->addField($field);
?>

You can add your own validation rules:

<?php
//add text field to form object
$field = new \Modules\developer\form\Field\Text(
array(
    'name' => 'firstField', //html "name" attribute
    'label' => 'First field', //field label that will be displayed next to input field
));
$field->addCustomValidator($customValidator); //$customValidator should extend \Modules\developer\form\Validator  class
$form->addField($field);
?>

PHP validation

To validate posted values use $form->validate($_POST) method which returns array of errors on failure and false on success.

<?php
//initialize the same form object as it was used to generate form
$errors = $form->validate($postData);

if ($errors) {
    //error
} else {
    //success
}
?>

Javascript validation

To validate form on client side please use jQuery Tools library which is included in ImpressPages CMS installation by default.

//'validator' is jqueryTools form validation library that is included in ImpressPages CMS core
//'validatorConfig' is default configuration value for that validator. You can modify validatorConfig or create your own one.
$ipForm.find('form').validator(validatorConfig);

This will do just initialization. To post data to the server for PHP validation more code is needed. Please refer to form widget example for complete code.

Change default form HTML

By default $form->render() method uses ip_cms/modules/developer/form/view/form.php view to generate HTML. You can change it to your own view file by passing view instance to render method

//Use this syntax anywhere
<?php
echo $form->render(new \Ip\View('relative/path/to/file.php'));
?>


//or this syntax within another view file
<?php
echo $form->render($this->subview('relative/path/to/file.php'));
?>

Here is an example that overrides default login form and adds a text on top of it. To prieview, copy extracted "modules" folder to your theme dir (eg. ip_themes/lt_pagan).

Write a comment

You must be logged in to comment.