【问题标题】:codeigniter form validation error message does not display [closed]codeigniter 表单验证错误消息不显示[关闭]
【发布时间】:2013-09-05 14:15:12
【问题描述】:

当我加载模型并从表中获取行时,我的表单验证错误不会在视图文件中显示消息。这是我的代码。

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

如果我排除这两行,我的表单可以正常工作

        /* Load Model */
        $this->load->model('book_category');

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

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

【问题讨论】:

  • 对不起我的错误。我错误地用 CI_controller 而不是 CI_Model 类继承了我的模型类( book_category )

标签: php forms codeigniter validation


【解决方案1】:

你应该使用validation_errors函数

<?php echo validation_errors(); ?>

文档 3.x:validation_errors

文档 2.x:form_validation

【讨论】:

  • 我同时包含 但是当我包含 $this->load->model('book_category'); 时它不起作用否则,如果我排除加载模型,它工作正常。
  • 请更改链接它不起作用。
  • @ankitsuthar 链接已更新,谢谢
  • 您能告诉我,我可以将这个validation_errors() 发送为flash_messages 吗? @宝来
  • @ankitsuthar 我猜,你可以这样使用:$this-&gt;session-&gt;set_flashdata('error', validation_errors());
【解决方案2】:

试着改成这样:

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

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

根据 CI 文档将模型首字母大写

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

这是来自他们的参考页面:

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

模型类的基本原型是这样的:

class Model_name extends CI_Model {

    function __construct()
    {
        parent::__construct();
    } 
} 

其中 Model_name 是您的班级名称。类名的第一个字母必须大写,其余部分小写。确保您的类扩展了基础模型类。

文件名将是您的类名的小写版本。例如,如果你的班级是这样的:

class User_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    } 

}

您的文件将是这样的:

application/models/user_model.php Loading a Model

您的模型通常会在您的控制器函数中加载和调用。要加载模型,您将使用以下函数:

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

【讨论】:

  • 我也试过了,但还是没有显示错误!
  • 对不起我的错误。我错误地用 CI_controller 而不是 CI_Model 类继承了我的模型类
  • 您只在创建类时将模型名称大写,而不是在访问它时。
【解决方案3】:

试试这个....

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-11
    • 1970-01-01
    • 2016-03-05
    相关资源
    最近更新 更多