【问题标题】:code igniter form_validation not workcodeigniter form_validation 不起作用
【发布时间】:2015-02-26 05:41:17
【问题描述】:

我的表单验证有问题,这只是标准的表单验证。

这很奇怪,即使我设置了条件,结果总是通过条件并运行“$this->model_item_management->tambah_item($item);”。

我的数据总是在没有通过表单验证的情况下保存。我尝试使用虚拟输入 $user_name 并将数据传递给 form_validation,但也没有用。感谢先进。这是我的代码。

public function index(){
       
       $this->load->helper(array('form', 'url'));
       $this->load->library('form_validation');
       
       //this is dummy post
       $user_name =  $this->input->post('user_name');
        
       //i try to pass an input into variable and use it on form validation
       $this->form_validation->set_rules('$user_name', 'username', 'required|min_length[4]');
       $this->form_validation->set_rules('namaitem', 'namaitem', 'required');
       $this->form_validation->set_rules('specitem', 'specitem', 'required');
       $this->form_validation->set_rules('masagaransi', 'masagaransi', 'required');
                
       if ($this->form_validation->run() == FALSE) {
			
           $this->load->view('template/header');
           $this->load->view('template/navigation');
           $this->load->view('pages/form_tambah_item');
           $this->load->view('template/footer');
		
       } else {
	   $this->load->view('pages/form_sukses_simpan_item');
       }
                
       $nama_item =  $this->input->post('namaitem');
       $spec_item =  $this->input->post('specitem');
       $garansi_item=  $this->input->post('masagaransi');
       
       $item = array(
           'NAMA_ITEM'=>$nama_item,
           'SPEC_ITEM'=>$spec_item,
           'MASA_GARANSI'=>$garansi_item
      );
       
        
        $this->model_item_management->tambah_item($item);
    }

<div class="container">
    
    <?php 
    
    
    $attributes = array('class' => 'form-horizontal', 'id' => 'form_tambah_item');
    echo form_open('kontrol_tambah_item',$attributes); ?>

    <div id="legend">
      <legend class="">Tambah Item Form</legend>
    </div>
    <?php echo form_label( 'username', 'user_name' ); ?>
            <?php echo form_input( 'user_name' ); ?>
    
    <div class="control-group">
      <!-- Nama item -->
      <label class="control-label"  for="namaitem">Nama Item</label>
      <div class="controls">
        <input type="text" id="namaitem" name="namaitem" placeholder="" class="input-xlarge">
        <p class="help-block">Masukkan nama dari items</p>
      </div>
    </div>
    
   <div class="control-group">
      <!-- spec item-->
      <label class="control-label"  >Spesifikansi Item</label>
      
      <div class="controls">
          <textarea class="input-xlarge" id="specitem" name="specitem" cols="40" rows="5"></textarea>
          <p class="help-block">Isi Spesifikasi Item</p>
      </div>
      </div>
    
    <div class="control-group">
      <!-- Masa garansi -->
      <label class="control-label"  for="masagaransi">Masa garansi</label>
      <div class="controls">
        <input type="text" id="masagaransi" name="masagaransi" placeholder="" class="input-xlarge">
        <p class="help-block">Isi Masa Garansi Item</p>
      </div>
    </div>
    
    <div class="control-group">
      <!-- Button -->
      <div class="controls">
          <button type="submit" class="btn btn-success">Tambah</button>
      </div>
    </div>
</div>

【问题讨论】:

  • 尝试将if ($this-&gt;form_validation-&gt;run() == FALSE)更改为if ($this-&gt;form_validation-&gt;run($this) == FALSE)

标签: php codeigniter validation error-handling


【解决方案1】:

改变

$this->form_validation->set_rules('$user_name', 'username', 'required|min_length[4]');

$this->form_validation->set_rules('user_name', 'username', 'required|min_length[4]');

【讨论】:

    【解决方案2】:

    当前:-
    $this->form_validation->set_rules('$user_name', 'username', 'required|min_length[4]');

    改为:-
    $this->form_validation->set_rules('user_name', 'username', 'required|min_length[4]');

    须知:
    上述函数接受三个参数作为输入:

    1. 字段名称 - 您为表单字段指定的确切名称。
    2.该字段的“人”名,将插入到错误消息中。例如,如果您的字段被命名为“用户”,您可以给它一个人名“用户名”。注意:如果您希望将字段名称存储在语言文件中,请参阅翻译字段名称。
    3.该表单域的验证规则。

    参考: https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#validationrules

    谢谢

    【讨论】:

      【解决方案3】:

      这太傻了,我忘了补充

      <?php echo validation_errors(); ?>
      

      在视图中,

      【讨论】:

        猜你喜欢
        • 2014-12-16
        • 1970-01-01
        • 1970-01-01
        • 2011-03-05
        • 2021-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多