【问题标题】:Form Validation in CodeIgniter?CodeIgniter 中的表单验证?
【发布时间】:2012-02-08 04:27:24
【问题描述】:

我正在使用 CodeIgniter 的表单验证,它工作正常,但是当表单验证失败时,它不会显示验证错误,通过使用 <?php echo validation_errors();?> 我正在使用

function insertProduct(){
    $this->load->library('form_validation');
    $this->form_validation->set_rules('pname','ProductName','trimirequired');
    if($this->form_validation->run()){
        $this->addProduct();
    }
    else{
        $this->load->model('inventory/stock');
    }

【问题讨论】:

  • 我看不到你在哪里使用validation_errors()...?不过,您确实有一个错字:trimirequired 应该是 trim|required
  • 我认为我使用的是'validation_errors()',长代码,这里不能给出,当我使用'trim|required'时,它不起作用
  • 你应该在 else{} 中加载视图,为什么要在那里加载模型?

标签: php codeigniter codeigniter-2


【解决方案1】:

在您看来,您应该有类似的内容(此示例单独显示错误);

<?php echo form_error('p_name'); ?>
<label for="p_name">Product Name</label>
<input type="text" id="p_name" name="p_name" value="<?php echo set_value('p_name'); ?>" />

【讨论】:

    【解决方案2】:

    您需要告诉控制器中的方法呈现表单验证成功/失败的视图。

    如果您将 insertProduct 方法更改为以下内容,它“应该”解决您的问题。

    function insertProduct(){
        $this->load->library('form_validation');
        $this->form_validation->set_rules('pname','ProductName','trimirequired');
        if($this->form_validation->run()){
            $this->addProduct();
            $this->load->view('{name_of_your_view}');
        } else{
            $this->load->model('inventory/stock');
            $this->load->view('{name_of_your_view}');
        }
    }
    

    “name_of_your_view”是您放置 validation_errors() 代码的视图。

    【讨论】:

      【解决方案3】:

      CodeIgniter 教程页面中的这个示例解释了如何验证提交的数据以在表单的标题处显示验证错误,就像您可能期望的那样:

      http://codeigniter.com/user_guide/tutorial/create_news_items.html

      创建函数的示例代码如下所示:

      public function create()
      {
          $this->load->helper('form');
          $this->load->library('form_validation');
      
          $data['title'] = 'Create a news item';
      
          $this->form_validation->set_rules('title', 'Title', 'required');
          $this->form_validation->set_rules('text', 'text', 'required');
      
          if ($this->form_validation->run() === FALSE)
          {
              $this->load->view('templates/header', $data);   
              $this->load->view('news/create');
              $this->load->view('templates/footer');
      
          }
          else
          {
              $this->news_model->set_news();
              $this->load->view('news/success');
          }
      }
      

      不过,正如其他人所说,您需要添加一个视图来处理成功并将它们返回到表单以在失败时显示错误。

      【讨论】:

        【解决方案4】:

        我们可以更改包含以下代码的行:

        $this->form_validation->set_rules('pname','ProductName','trimirequired');
        

        到:

        $this->form_validation->set_rules('pname','ProductName','trim|required');
        if($this->form_validation->run($this) == false)
        {
           $this->addProduct();
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-02-06
          • 2016-09-05
          • 2021-10-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多