How to Form-Validation in CodeIgniter?
1.we define form_validation in autoload:-
$autoload['libraries'] = array('database', 'email', 'session','form_validation');
2.we define form in Helper:-
$autoload['helper'] = array('url', 'form');
3.After that we create Controller and load these file:-
- class Validation extends CI_Controller {
- public function __construct()
- {
- parent::__construct();
- $this->load->helper(array('form', 'url'));
- $this->load->libraries('form_validation');
- }
- }
- public function validation_msg()
- {
- $this->form_validation->set rules('first name', 'First Name', 'required',
- $this->form_validation->set rules('second name', 'secondName', 'required',
- if ($this->form_validation->run() == FALSE)
- {
- $this->load->view('formsuccess');
- }
- else
- {
- echo "Success Message";
- }
- }
4.Create a View:-
- <form action = " " method = "">
- <?php echo validation_errors(); ?>
- <h5>First field</h5>
- <input type = "text" name = "first field" value = "" />
- <h5>Second field field</h5>
- <input type = "text" name = "second field" value = "" />
- <div><input type = "submit" value = "Submit" /></div>
- </form>
Tagged: