Topics

Form object

The form object is used to generate form's HTML and validate user's input. First, you need to create a form object, and then add some fields to it.

Create a form object

<?php
$form = new \Ip\Form();

Add a text field to a form 

$field = new \Ip\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);

Output form's HTML 

echo $form->render();

Fields

See /Ip/Form/Field directory, containing files with field classes. Following fields are available:

Field name

Purpose Field specific options

Antispam

Hidden field for antispam validation

Blank

Blank field

Captcha

CAPTCHA field

Checkboxes

Multiple checkbox field

vales

Checkbox

Checkbox field

checked

text

Color

Color picker

Csrf

Hidden field for validation against CSRF attack

Currency

For price fields

Email

E-mail input field

File

File upload button

Hidden

Hidden HTML field

Info

Informational field. Put any HTML you like. html

Number

Numeric value input

Password

Password field

Radio

Radio buttons

Range

Range field

RepositoryFile

Add a repository file. (Requires login to administration page and form environment set to admin mode.)

secure

fileLimit

preview

RichText

Rich text field

Select

Select field values

Submit

Submit button

Text

Text input field

Textarea

HTML text area field

Url

URL input field

 Each field can have following attributes:

  • name HTML name attribute,
  • label - text label displayed next to input field,
  • note  - text displayed near input field,
  • hint - text displayed on mouse over the input field,
  • value - default field value,
  • css - CSS class.

Fieldsets

Fieldsets can be used to group form fields, by drawing a box around the related elements.

Group input fieldS using fieldsets

<?php
    $form->addFieldset(new \Ip\Form\Fieldset('My fieldset 1'));
    $form->addField($field1);
    $form->addField($field2);
    $form->addField($field3);

    $form->addFieldset(new \Ip\Form\Fieldset('My fieldset 2'));
    $form->addField($field4);
    $form->addField($field5);
    $form->addField($field6);

Add validation rules

Use $form->addValidator($validator) method on fields to add validation rules. As a validator you can pass:

  • String - if you use one of default validators that doesn't need any parameters, like EmailRequired or Number
  • Array - if you use one of default validators that needs parameters to be set. First array element is a string representing a name of a validator (e.g., Regex). A second element is a parameter passed to a validator (e.g., /^[0-9]*$/)
  • Object - your custom validation object that extends \Ip\Form\Validator class. 
<?php

$field->addValidator('Required');
$field->addValidator('Regex', '/^[0-9]*$/');
$field->addValidator($customValidator);

Form object has two hidden validation fields added by default: Antispam and Csrf. You can remove them using following functions.

$form->removeSpamCheck()
$form->removeCsrfCheck()

HTML attributes

Use addClass method to add a specific class to a form.

$form->addClass('cssClassName');

Also, you can add other attributes, like id, etc.

$form->addAttribute('id', 'myCustomForm');

Form's environment

Set form's environment to admin 

<?php $form->setEnvironment(\Ip\Form::ENVIRONMENT_ADMIN); ?>

Set form's environment to public

<?php $form->setEnvironment(\Ip\Form::ENVIRONMENT_PUBLIC); ?>

Field layouts (since 4.1.2)

Each input field on ImpressPages is surrounded by some additional HTML which gives freedom to style form using CSS. Label, error, notes, etc. are printed alongside. You can control it by overriding form's view files (see above) or use field layout. There are three layouts available:

  • \Ip\Form\Field:LAYOUT_DEFAULT
  • \Ip\Form\Field:LAYOUT_NO_LABEL
  • \Ip\Form\Field:LAYOUT_BLANK

Set the layout as follow

$field = new \Ip\Form\Field\Text(
    array(
        'layout' => \Ip\Form\Field::LAYOUT_NO_LABEL,
        'name' => 'firstField', // HTML "name" attribute
    )
);

or 

$field->setLayout(\Ip\Form\Field::LAYOUT_NO_LABEL);

See also

comments powered by Disqus