php codeigniter 表单验证错误消息不显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18573746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:50:04  来源:igfitidea点击:

codeigniter form validation error message does not display

phpformscodeignitervalidation

提问by Aneeq Tariq

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 ?

比我的验证显示错误。我不知道问题出在哪里?

回答by Bora

You should use validation_errorsfunction

你应该使用validation_errors函数

<?php echo validation_errors(); ?>

Documentation 3.x: validation_errors

文档 3.x: validation_errors

Documentation 2.x: form_validation

文档 2.x:form_validation

回答by trrrrrrm

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

模型第一个字母按照 CI 文档大写

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

参考: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.

模型类存储在您的 application/models/ 文件夹中。如果您需要这种类型的组织,它们可​​以嵌套在子文件夹中。

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.

其中 Model_name 是您的类的名称。类名的第一个字母必须大写,其余的小写。确保您的类扩展了基本 Model 类。

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

回答by Giri Annamalai M

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