Today I am going to tell you how to do the form validation.
Refer the lesson # five before do this.
What is form validation.Form validation is a kind of thing where we define
a set of rules and the user has to obay for those user validations.
It is simple to do user validations in CI.
It uses Form_validation library to do it.
***So you have to modify our getcontroller.php file as follows.
Here is the new getcontroller.php code.
<?php
class getcontroller extends CI_Controller{
public function getview(){
$this->load->view('getview');
}
public function reg(){
$this->load->library('form_validation');
$this->form_validation->set_rules('id', 'id', 'trim|required|min_length[4]|xss_clean');
$this->form_validation->set_rules('name', 'User Name', 'trim|required|min_length[4]|xss_clean');
if($this->form_validation->run()==FALSE){
echo '<script>alert("You have not fill the form correctly.Recheck");</script>';
$this->load->view('getview');
}
else{
$this->load->model('getmodel');
$this->getmodel->getreg();
}
}
}
?>
Ok.Now you have to call the use validation in getview.php
So add these codes to getview.php
<?php echo validation_errors('<p >'); ?> and save it.
Now go to web browser and type below in address bar
http://localhost/codeigniter/index.php/getcontroller/reg
and you will see an inteface.
Now in the interface donot fill the Id field and sumit button and you will see it returns validation errors.
Refer the lesson # five before do this.
What is form validation.Form validation is a kind of thing where we define
a set of rules and the user has to obay for those user validations.
It is simple to do user validations in CI.
It uses Form_validation library to do it.
***So you have to modify our getcontroller.php file as follows.
Here is the new getcontroller.php code.
<?php
class getcontroller extends CI_Controller{
public function getview(){
$this->load->view('getview');
}
public function reg(){
$this->load->library('form_validation');
$this->form_validation->set_rules('id', 'id', 'trim|required|min_length[4]|xss_clean');
$this->form_validation->set_rules('name', 'User Name', 'trim|required|min_length[4]|xss_clean');
if($this->form_validation->run()==FALSE){
echo '<script>alert("You have not fill the form correctly.Recheck");</script>';
$this->load->view('getview');
}
else{
$this->load->model('getmodel');
$this->getmodel->getreg();
}
}
}
?>
Ok.Now you have to call the use validation in getview.php
So add these codes to getview.php
<?php echo validation_errors('<p >'); ?> and save it.
Now go to web browser and type below in address bar
http://localhost/codeigniter/index.php/getcontroller/reg
and you will see an inteface.
Now in the interface donot fill the Id field and sumit button and you will see it returns validation errors.
That is all about validation in CI.
-Ujitha Maunuja Sudasingha-
-Ujitha Maunuja Sudasingha-