You are on page 1of 7

https://phpgurukul.

com/validation-rules-
to-a-config-file-in-codeigniter
Validation rules to a config file in CodeIgniter
You can save sets of rules to a config file. To start, create a new file called
form_validation.php, inside the application/config/ directory. The rules must be
contained within a variable $config, as with all other config files.
The rules from our Signup form would now appear as follows:
<?php
$config = array(
array(
'field' => 'fullname',
'label' => 'Full Name',
'rules' => 'required'
),
array(
'field' => 'email',
'label' => 'Email Address',
'rules' => 'required|valid_email'
),
array(
'field' => 'phonenumber',
'label' => 'Phone Number',
'rules' => 'required|numeric|exact_length[10]'
), https://phpgurukul.com/validation-rules-
to-a-config-file-in-codeigniter
array(
'field' => 'username',
'label' => 'User name',
'rules' =>
'required|alpha_numeric|min_length[6]|max_length[12]|is_unique[tblusers.username]'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'required|min_length[6]'
),
array(
'field' => 'cpassword',
'label' => 'Confirm Password',
'rules' => 'required|min_length[6]|matches[password]'
)
);

Creating sets of rules


If you have more than one form that needs validating, you can create sets of
rules. To do this, you need to place the rules into ‘sub-arrays’. The rules for our
contact form would appear as follows when we place it into a set:

https://phpgurukul.com/validation-rules-
to-a-config-file-in-codeigniter
$config = array(
'signup' => array(
array(
'field' => 'fullname',
'label' => 'Full Name',
'rules' => 'required'
),
array(
'field' => 'email',
'label' => 'Email Address',
'rules' => 'required|valid_email'
),
array(
'field' => 'phonenumber',
'label' => 'Phone Number',
'rules' => 'required|numeric|exact_length[10]'
),

https://phpgurukul.com/validation-rules-
to-a-config-file-in-codeigniter
array(
'field' => 'username',
'label' => 'User name',
'rules' =>
'required|alpha_numeric|min_length[6]|max_length[12]|is_unique[tblusers.usern
ame]'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'required|min_length[6]'
),
array(
'field' => 'cpassword',
'label' => 'Confirm Password',
'rules' => 'required|min_length[6]|matches[password]'
)
)
);
https://phpgurukul.com/validation-rules-
to-a-config-file-in-codeigniter
This method allows you to have as many sets of rules as you need.
Calling a specific set of rules
You need to specify the rule set that you want to validate the form against, on
the run function. Our edited controller would now look like this:
if($this->form_validation->run('signup') == FALSE)
{
// load the signup form
}
else
{
// signup for submitted successfully
}

https://phpgurukul.com/validation-rules-
to-a-config-file-in-codeigniter
For More Details Visit-
https://phpgurukul.com/validation-rules-to-a-config-file-in-
codeigniter/

https://phpgurukul.com/validation-rules-
to-a-config-file-in-codeigniter

You might also like