codeigniter form validation error message does not display

  • Added:
  • |
  • In: Basic PHP

my form validation error does not show messages in view file when i load model and get row from tables. here is my code.

 $this->form_validation->set_rules('bookCategoryId', 'Book SubCategory Id', 'trim|required'); $this->form_validation->set_rules('bookSubCategoryId', 'Book SubCategory Id', 'trim|required'); $this->form_validation->set_rules('bookSubCategoryName', 'Book SubCategory Name', 'trim|required'); if ($this->form_validation->run() == FALSE) { /* Load Model */ $this->load->model('book_category'); /* Get Categories */ $template_data['mainContentData']['book_categories'] = $this->book_category->get_all_categories(); /* set view page to be called */ $template_data['mainContent'] = 'admin_add_book_subcategory'; /* Load Template */ $this->template($template_data); } 

My form works fine if i exclude these two line

 /* Load Model */ $this->load->model('book_category'); /* Get Categories */ $template_data['mainContentData']['book_categories'] = $this->book_category->get_all_categories(); 

than my validations shows error. I dont know where is the problem ?

This Question Has 4 Answeres | Orginal Question | Aneeq Tariq

You should use validation_errors function

<?php echo validation_errors(); ?> 

Documentation 3.x: validation_errors

Documentation 2.x: form_validation

Try to change it to this:

 $this->load->model('Book_category'); /* Get Categories */ $template_data['mainContentData']['book_categories'] = $this->Book_category->get_all_categories(); 

Models first letter capitalized as per CI documentation


This is from their reference page:

Model classes are stored in your application/models/ folder. They can be nested within sub-folders if you want this type of organization.

The basic prototype for a model class is this:

class Model_name extends CI_Model { function __construct() { parent::__construct(); } } 

Where Model_name is the name of your class. Class names must have the first letter capitalized with the rest of the name lowercase. Make sure your class extends the base Model class.

The file name will be a lower case version of your class name. For example, if your class is this:

class User_model extends CI_Model { function __construct() { parent::__construct(); } } 

Your file will be this:

application/models/user_model.php Loading a Model 

Your models will typically be loaded and called from within your controller functions. To load a model you will use the following function:


Try this....

 /* Load Model */ $this->load->model('book_category'); /* Get Categories */ $template_data['mainContentData']['book_categories'] = $this->book_category->get_all_categories(); /* set view page to be called */ $template_data['mainContent'] = 'admin_add_book_subcategory'; $this->form_validation->set_rules('bookCategoryId', 'Book SubCategory Id', 'trim|required'); $this->form_validation->set_rules('bookSubCategoryId', 'Book SubCategory Id', 'trim|required'); $this->form_validation->set_rules('bookSubCategoryName', 'Book SubCategory Name', 'trim|required'); if ($this->form_validation->run()) { print_r($_POST); exit; } /* Load Template */ $this->template($template_data); 

Sorry My mistake. i mistakenly inherit my model class ( book_category ) with CI_controller Instead of CI_Model class

I am...

Sajjad Hossain

I have five years of experience in web development sector. I love to do amazing projects and share my knowledge with all.

Connect Social With PHPAns