2017-09-26 16 views
-1

マイビュー:PHP CodeIgniterのフォームバリデーション

<div class="row eva-add-mainbox"> 
      <div class="col s3 eva-aditem-leftbox"> 
       <p>Product Item Name</p> 
      </div> 
      <div class="col s9"> 
       <div class="input-field col s12"> 
        <input type="text" name="pname" class="validate" id="proName"> 
        <label for="icon_prefix">Product Item Name</label> 
       </div> 
      </div> 
     </div> 

私のコントローラ: - :

製品の説明フィールドが必要です

$this->form_validation->set_rules('itemCode', 'Item Code', 'trim|required|min_length[2]|max_length[38]|xss_clean'); 
    $this->form_validation->set_rules('pDescription', 'Product Description', 'trim|required|min_length[2]|max_length[38]|xss_clean'); 
    $this->form_validation->set_rules('productPrice', 'Price', 'required|max_length[12]|regex_match[/^[0-9.]+$/]|xss_clean'); 
    $this->form_validation->set_rules('proQuantity', 'Quantity', 'max_length[12]|numeric|greater_than[0]|xss_clean'); 
    $this->form_validation->set_rules('pname', 'Name', 'trim|required|min_length[2]|max_length[38]|xss_clean'); 

    if($this->form_validation->run() == TRUE) { 
     if ($_FILES["file"]["name"] != '') { 
      $test = explode('.', $_FILES['file']['name']); 
      $extension = end($test); 
      $name = rand(100, 999) . '.' . $extension; 
      $location = './images/' . $name; 
      move_uploaded_file($_FILES['file']['tmp_name'], $location); 
      $b = $this->session->userdata('business'); 
      $pdata['product_code'] = $this->input->post('itemCode'); 
      $pdata['title'] = $this->input->post('pName'); 
      $pdata['discription'] = $this->input->post('description'); 
      $pdata['business_id'] = $b->biz_id; 
      $pdata['price'] = $this->input->post('price'); 
      $proQuantity = $this->input->post('proQuantity'); 
      $pdata['image'] = $location; 
      $pAttribute = $this->input->post('pAttribute'); 
      $pValue = $this->input->post('pValue'); 
     } 

     $result = $this->product_model->setProductData($pdata, $pAttribute, $pValue, $proQuantity); 
    }else 
    { 
     echo validation_errors(); 
    } 

は、私はエラーを取得します。

価格フィールドは必須です。

[名前]フィールドは必須です。

なぜそれが起こるのか説明できますか?検証で

+1

来るThatsなぜこれら三つのフィールドを検証? '$ this-> form_validation-> set_rules( 'itemCode'、 'Item Code'、 'trim |必須| min_length [2] | max_length [38] | xss_clean');$ this-> form_validation-> set_rules( 'pDescription'、 'Product Description'、 'trim |必須| min_length [2] | max_length [38] | xss_clean');など。それらのフィールドがあなたのHTMLフォームにないからといって、コントローラがそれらのフィールドを送信することを期待しているわけではありません。いくつかのフィールドのみを入力するフォームのカットダウンバージョンが必要な場合は、独自の検証ルールを持つ別個のViewModelが必要です。 – ADyson

+0

これは検証ルールで必須に設定されているためです –

答えて

1

あなたはそれらのフィールドを必要とするルールを設定しているため、そのは

$this->form_validation->set_rules('pDescription', 'Product Description', 'trim|required|min_length[2]|max_length[38]|xss_clean'); 
$this->form_validation->set_rules('proQuantity', 'Quantity', 'max_length[12]|numeric|greater_than[0]|xss_clean'); 
    $this->form_validation->set_rules('pname', 'Name', 'trim|required|min_length[2]|max_length[38]|xss_clean'); 
関連する問題