Today I am bought you that how you can add the Custom Validation in the form your are working on , as Codeigniter define their validation in their in-built form validation class but what if i wan't to add my own custom rules in the validation of a form.
Below is the screenshot of a form which i have already made and in this i am going to tell you how you can add your custom validation rules in the form.
I have made a class name form and in this we are calling the index function as you all know that the codeigniter always calls the index function of any controller so in this we are calling the index function and inside this function we are going to do the validation
<?php
class Form extends CI_Controller {
public function index()
{
// Validation code come here
}
}
?>
Now from here i am going to tell you how we can add our custom rules in Codeigniter Form.
First of all we have to load the library which we required in the form validation and same we declare in the code in the above . the line which we add is given below
$this->load->helper(array('form', 'url'));
// this code is use the helper for the form which we require.
$this->load->library('form_validation');
// the above line of code tells the codeigiter to load the library for Form Validation which we require in this problem .
After adding thetwo lines the code comes like this.
<?php
class Form extends CI_Controller {
public function index()
{
$this->load->helper(array('form','url,'));
$this->load->helper('form_validation');
}
}
?>
Now, After loading the library and the helper class we need to do validation for our fields.
The syntax here we use is given below
$this->form_validation->set_rules('Field Name','Field Title','Rules')
Field Name is the name of the text field which you have taken in the form.
Field Title is the title which you want to given while validating the text Field.
Rules is the Rules for the validation you define for the particular field such as require or valid email.
So in our form in the screenshort above we have three field namd User Name, Password and the last Field is Email Address. We have taken username,password and the email as their field name in the form which we are going to validate.
So now we add aour rules for these fiels
For Username we write the code as given below,
$this->form_validation->set_rules('username','User Name','trim|required|min_length[6]');
As for the User Name we write the same code for validation for the other fields and when we combine the code your can see the code like this as shown below.
<?php
class Form extends CI_Controller {
public function index()
{
$this->load->helper(array('form','url,'));
$this->load->helper('form_validation');
$this->form_validation->set_rules('username','User Name','trim|required|min_length[6]'); // Validation for username field
$this->form_validation->set_rules('password','Password ','trim|required'); // Validation for password field
$this->form_validation->set_rules('email','Email Id','trim|required|valid_email'); / Validation for email Id field
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform'); // redirected to back to the form page
}
else
{
echo 'Success!'; // This is the success message when validation rules fulfilled.
}
}
}
?>
After when you try to submitted the form you get the error in form like this as shown in the screenshort given below
If you want to change the field name you are free to change and in the form validation rules you can change the field name which field you are going to validate in the form.


No comments:
Post a Comment