Retired Documentation
You are using the documentation for version 2.11.9. Go here for the latest version or check here for your available upgrades to the latest version.
Form Validation Class¶
Usage of Form Validation Class¶
-
class
Form_validation
¶ 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.
Setting Reference Values¶
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.
-
Form_validation::
set_old_value
($key[, $value = ''])¶ Is used to set a reference value for a given key:
ee()->form_validation->set_old_value($key, $value)
Parameters: - $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
Return type: Void
-
Form_validation::
old_value
($key)¶ Can be used to retrieve a reference value that was previously set. This method is primarily used internally:
ee()->form_validation->old_value($key)
Parameters: - $key (string) – Name of the input to retrieve
Returns: Data of the value associated with
$key
Return type: String
User Specific Rules¶
A few rules are provided to check for illegal characters, validate input lengths, and check for duplicates on member fields.
valid_username¶
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]');
valid_screen_name¶
Works the same way as the valid_username rule. Expects that a reference
value for 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');
valid_user_email¶
Identical to the previous rules, but expects the reference value to be
set for email
:
// 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]');
valid_password¶
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]');
Prepping Methods¶
These are convenience methods to help sanitize and validate frequently used input data patterns.