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:-
  1. class Validation extends CI_Controller {
  2. public function __construct()
  3. {
  4. parent::__construct();
  5. $this->load->helper(array('form', 'url'));
  6. $this->load->libraries('form_validation');
  7. }
  8. }
  9. public function validation_msg()
  10. {
  11. $this->form_validation->set rules('first name', 'First Name', 'required',
  12. $this->form_validation->set rules('second name', 'secondName', 'required',
  13. if ($this->form_validation->run() == FALSE)
  14. {
  15. $this->load->view('formsuccess');
  16. }
  17. else
  18. {
  19. echo "Success Message";
  20. }
  21. }
4.Create a View:-
  1. <form action = " " method = "">
  2. <?php echo validation_errors(); ?>
  3. <h5>First field</h5>
  4. <input type = "text" name = "first field" value = "" />
  5. <h5>Second field field</h5>
  6. <input type = "text" name = "second field" value = "" />
  7. <div><input type = "submit" value = "Submit" /></div>
  8. </form>


Tagged:
Sign In or Register to comment.