Form Validation Class¶
The Form Validation class provides comprehensive validation framework to minimize the amount of code you need to write. It provides a toolset for validating user input, showing validation errors, and repopulating form fields.
The base functionality for the Form Validation class is inherited from the corresponding CodeIgniter library—refer to the Form Validation documentation in the CodeIgniter user guide for details.
For member screen names, member usernames, and member email addresses the Form Validation class needs a reference value. This value will be ignored when checking for duplicates.
set_old_value($key[, $value = ''])¶
Is used to set a reference value for a given key:
- $key (mixed) – Name of input or associative array containing input names as the keys and their values as the values
- $val (string) – The value to set
Can be used to retrieve a reference value that was previously set. This method is primarily used internally:
- $key (string) – Name of the input to retrieve
Data of the value associated with
A few rules are provided to check for illegal characters, validate input lengths, and check for duplicates on member fields.
Checks the username for invalid characters, as well as making sure no
duplicates exist. It takes an optional parameter to indicate if this is
a new user. Expects that a reference value for
username was set
before running the validation:
ee()->form_validation->set_rules('username', 'Username', 'required|valid_username'); ee()->form_validation->set_rules('username', 'Username', 'required|valid_username[new]');
Works the same way as the valid_username rule. Expects that a reference
screen_name was set before running the validation:
ee()->form_validation->set_old_value('screen_name', 'Bob'); ee()->form_validation->set_rules('screen_name', 'Screen Name', 'required|valid_screen_name');
Identical to the previous rules, but expects the reference value to be
// Note: no reference value is needed if this is a new user ee()->form_validation->set_rules('email', 'lang:email', 'required|valid_user_email[new]');
Checks the submitted password for weaknesses and illegal characters. Takes the name of a username field as a parameter, which is used to prevent a password that is based on the username. This rule does not check user credentials:
ee()->form_validation->set_rules('password', 'lang:password', 'required|valid_password[username]');
These are convenience methods to help sanitize and validate frequently used input data patterns.