2016-08-05 69 views
0

我的codeigniter驗證錯誤不顯示某人可以提供幫助嗎? 我的代碼是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'); 
     } 

} 

,我已經加入 回波VALIDATION_ERRORS();在我看來,行爲的形式是產品/ addProduct。

+0

因爲你必須在你的函數中通過驗證錯誤 –

+0

@Nikhil Vaghla我必須在另一個函數中傳遞驗證錯誤? –

+0

刪除第一個$ this-> load-> view('product/addProduct'); ($ this-> form_validation-> run()== FALSE) –

回答

0

上查看例如

<?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自己的基本網址現在需要CI3版本。

1

試試這個對你有用。

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

OR

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

謝謝。但是請你解釋一下我的代碼中的錯誤。 –

+1

在你的代碼中,你不能在其他部分傳遞錯誤。你需要在你的視圖中發佈你的錯誤 –

+0

我已經使用了回聲驗證錯誤();在我看來 –