【问题标题】:Codeigniter validation errors are not displayingCodeigniter 验证错误未显示
【发布时间】:2016-12-11 13:07:00
【问题描述】:

我的 codeigniter 验证错误没有显示有人可以帮忙吗? 我的代码是

public function addProduct(){
    $this->load->view('header', $this->data);
    $this->load->view('product/addProduct');
    $this->load->view('footer');

        $this->form_validation->set_rules('productName', 'Product Name', 'required|trim');
        $this->form_validation->set_rules('productPrice', 'Product Price', 'required|trim');
        if (!$this->form_validation->run() == FALSE)
        {
            // some stuff on validation success
        }
        else{
            $this->load->view('product/addProduct');
        }

}

我已经添加了 回声验证错误();在我看来,表单的操作是 product/addProduct。

【问题讨论】:

  • 因为你必须在你的函数中通过验证错误
  • @Nikhil Vaghla 我必须在另一个函数中传递验证错误?
  • 删除第一个 $this->load->view('product/addProduct');
  • 这样使用 if ($this->form_validation->run() == FALSE) { $this->load->view('product/addProduct'); } else{ // 关于验证成功的一些东西 }

标签: php codeigniter


【解决方案1】:

查看示例

<?php echo validation_errors('<div class="error">', '</div>'); ?>

<!-- lower case for the controller name on form open -->

<?php echo form_open_multipart('product/addProduct');?>

<h5>productName</h5>
<input type="text" name="productName" value="<?php echo set_value('productName'); ?>" size="50" />

<h5>productPrice</h5>
<input type="text" name="productPrice" value="<?php echo set_value('productPrice'); ?>" size="50" />

<div><input type="submit" value="Submit" /></div> 

<?php echo form_close();?>

控制器

确保您的文件名和类名如下所示,其中首字母仅大写

Guide

文件名:Product.php

<?php

class Product extends CI_Controller {

 public function __construct() {
    parent::__construct();
    $this->load->library('form_validation');
    $this->load->helper('form');
    $this->load->helper('url');
 }

 public function addProduct(){

    // You can get data from here also

    $this->data['some'] = 'Blah';

    $this->form_validation->set_rules('productName', 'Product Name', 'required|trim');
    $this->form_validation->set_rules('productPrice', 'Product Price', 'required|trim');

    // remove your !

    if ($this->form_validation->run() == FALSE){

        // You can just display view in this area you do not have to load it multiple times

        $this->load->view('header', $this->data);
        $this->load->view('product/addProduct');
        $this->load->view('footer');

    } else {

        // some stuff on validation success
    }

 }

}

同时检查你是否已经在 config.php 中设置了你的基本 url,现在 CI3 版本需要这个。

【讨论】:

    【解决方案2】:

    试试这个它对你有用。

    form_error() 函数返回您的表单错误。

            $post_fields = $this->input->post();
            $data['msg'] = '<ul>';
            foreach ($post_fields as $k => $v) {
                if (form_error($k))
                    $data['msg'] .= "<li>" . strip_tags(form_error($k)) . "</li>\n";
            }
            $data['msg'].='</ul>';
            $this->load->view('product/addProduct',$data);
    

    echo validation_errors();//this function also return form  error.
    

    【讨论】:

    • 谢谢。但是请您解释一下我的代码中的错误。
    • 在您的代码中,您不能在其他部分传递错误。您需要在视图中发布您的错误
    • 如果您对答案感到满意,请接受答案并投票。
    猜你喜欢
    • 1970-01-01
    • 2017-10-13
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多