I want to set validation rule in ci so that all the fields are set to "required". Should I set the validation rule individually for all the fields or is there any way to set validation rule to all fields at once?
Codeigniter to validation rules set different ways having
1)Setting Validation Rules individually ex:
$this->form_validation->set_rules('username', 'Username', 'required');
2) Setting Rules Using an Array (Group)
$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->form_validation->set_rules($config);
3) Saving Sets of Validation Rules to a Config File
A nice feature of the Form Validation class is that it permits you to store all your validation rules for your entire application in a config file. You can organize these rules into "groups". These groups can either be loaded automatically when a matching controller/function is called, or you can manually call each set as needed. How to save your rules
To store your validation rules, simply create a file named form_validation.php in your application/config/ folder. In that file you will place an array named $config with your rules. As shown earlier, the validation array will have this prototype:
Creating Sets of Rules
In order to organize your rules into "sets" requires that you place them into "sub arrays". Consider the following example, showing two sets of rules. We've arbitrarily called these two rules "signup" and "email". You can name your rules anything you want:
$config = array(
'signup' => array(
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'required'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'required'
),
),
'email' => array(
array(
'field' => 'emailaddress',
'label' => 'EmailAddress',
'rules' => 'required|valid_email'
),
)
);
Calling a Specific Rule Group
In order to call a specific group you will pass its name to the run() function. For example, to call the signup rule you will do this
:
if ($this->form_validation->run('signup') == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
you need more detatils please check it once
https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html