Form Validation
GoodForm plays nice with CodeIgnites Form Validation library. If needed, you can access the form_validation library instance directly by referencing $this->form_validation.
GoodForm has the following methods to control validation.
- Setting Validation Rules
- Setting Validation Rules Using an Array
- Setting Validation Rules Using Input Attributes
- Setting Custom Error Messages
- Running Form Validation
- Checking if a Form was Submitted
set_rules(mixed field [, string name, string rules])
The GoodForm set_rules() function works identical to the Form Validation library.
Setting Validation Rules
$this->goodform->set_rules('email', 'Email', 'required');
Setting Multiple Rules with an Array.
$config = array(
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'required'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'required'
),
array(
'field' => 'passconf',
'label' => 'Password Confirmation',
'rules' => 'required'
),
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'required'
)
);
$this->goodform->set_rules($config);
Setting Validation Rules Using Input Attributes
Validation rules can also be set by passing a validation attribute to a Input Method. Like the Form Validation library, define multiple rules with a pipe: 'rule1|rule2'.
Example
$attr = array(
'name' => 'email',
'label' => 'Email'
'validation' => 'required|valid_email'
);
$this->goodform->text($attr);
When setting validation rules, you also need to specify the label attribute. This will result in the label being used in any error messages generated. If no label is defined, the field name will be used instead
set_message(string rule [, string message)
Where rule corresponds to the name of a particular rule, and Error Message is the text you would like displayed.
If you include %s in your error string, it will be replaced with the "label" name you used for your field when you set your rules.
$this->goodform->set_message('required', 'Your custom message here');
run()
The run() method will process posted information and apply the validation rules you have set. GoodForm automatically updates form values and adds any inline errors to your fields. For this reason run should be called after you have applied all rules to the form and before calling generate().
It returns TRUE or FALSE depending if the form passed all validation checks.
Since you haven't told the Form Validation class to validate anything yet, it returns FALSE (boolean false) by default. The run() function only returns TRUE if it has successfully applied your rules without any of them failing.
submitted()
This additional GoodForm method returns TRUE if the form was submitted. This can be useful if you only want to perform a given action if this form was submitted. Each time you add an input field to GoodForm it keeps an array of all the field names, then checks if any of these values exist in the php $_POST array. Handy if you have more than one form on a page.
Example
// add form data and prep validation rules
// ...
// check if the form was submitted
if($this->goodform->submitted())
// run validation
if($this->goodform->run() == FALSE)
// failure
else
// success
else
// do nuthin'
// ...
// generate form and dispay view