CodeIgniter Form Validation
codeigniterCodeIgniter provides a form validation library that helps you to validate form fields by writing minimal code. You can set validation rules for each form field, and the library will automatically check the user’s input against these rules when the form is submitted.
To use the form validation library, you will first need to load it in your controller:
$this->load->library('form_validation');
Next, you can set the validation rules for your form fields. You can set the rules using an array or by chaining them using the set_rules() function.
// Array syntax
$config = array(
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'required'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'required',
'errors' => array(
'required' => 'You must provide a %s.',
),
),
array(
'field' => 'passconf',
'label' => 'Password Confirmation',
'rules' => 'required'
),
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'required'
)
);
$this->form_validation->set_rules($config);
// Chaining syntax
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');
You can also set custom error messages for each rule using the errors array.
Once you have set the validation rules, you can validate the form by calling the run() function:
if ($this->form_validation->run() == FALSE) {
// Validation failed
$this->load->view('myform');
} else {
// Validation succeeded
$this->load->view('formsuccess');
}
Other Article on Tag codeigniter
- - CodeIgniter Application Architecture
- - CodeIgniter Configuration
- - CodeIgniter connect database
- - CodeIgniter Error Handling
- - CodeIgniter File Uploading
- - CodeIgniter Form Validation
- - CodeIgniter Installing
- - CodeIgniter Sending Email
- - CodeIgniter Session Management
- - codeigniter vs cakephp