Form Generation Library


Validation

After the form was submit, all elements will be validated with the rules you have set in the configuration file or supplied with the elements.

The validation is being called automatically when outputting the (submitted) form via get() or get_array():

$this->form->get();

// or
$this->form->get_array();

In order to do some additional stuff after the form was validated but before the form was output you can use the following syntax:

$this->form
->open() ->...;
// only validate the form $this->form->validate();

if ($this->form->valid)
{
// do stuff
}
// return the generated form html $data['form'] = $this->form->get();

Process Order

Validation is processed (and therefore errors are sorted) in the following order:

1. Form Elements
2. Captcha / Recaptcha
3. File Uploads

If all of the above elements passed validation, models are being called - otherwise errors will be generated and the form will be re-populated.

4. Model(s)

If none of the models flags an error via

// in the model the form is accessed via $form (instead of $this->form)
$form->add_error('element', 'Error Message');

the entire form is considered valid - otherwise (model) errors will be generated and the form will be re-populated.

Default Validation Rules

You can set default rules in the configuration file and use ^ as a placeholder that will be substituted with any rules defined subsequently in the replacement hierarchy.

Example

$config['default'] = array(
   'text' => array(
       'rules' => 'trim^xss_clean'
   )
);

with

$this->form->text('Username', 'username', 'required|min_length[6]');

will produce the following validation rules

trim|required|min_length[6]|xss_clean

because ^ was substituted with 'required|min_length[6]'

If you don't use the ^ placeholder rules will be appended to subsequent rules by default. In order to prepend subsequent rules you need to use the placeholer like this: '^rules'