Home » Php » php – codeigniter form validation error message does not display

php – codeigniter form validation error message does not display

Posted by: admin July 12, 2020 Leave a comment

Questions:

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 ?

How to&Answers:

You should use validation_errors function

<?php echo validation_errors(); ?>

Documentation 3.x: validation_errors

Documentation 2.x: form_validation

Answer:

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

Reference: http://ellislab.com/codeigniter/user-guide/general/models.html

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:

$this->load->model('Model_name');

Answer:

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);