我的表单验证有问题,这只是标准表单验证。
这很奇怪,即使我设定了条件,结果总是通过条件并运行" $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>
&#13;
答案 0 :(得分:2)
当前: -
$ this-&gt; form_validation-&gt; set_rules(&#39; $ user_name&#39;,&#39; username&#39;,&#39; required | min_length [4]&#39;);
更改为: -
$ this-&gt; form_validation-&gt; set_rules(&#39; user_name&#39;,&#39;用户名&#39;,&#39; required | min_length [4]&#39;);
< / p>
要了解的事情:
上述功能需要三个参数作为输入:
1.字段名称 - 您给出表单字段的确切名称
2.A&#34;人类&#34;此字段的名称,将插入错误消息中。例如,如果您的字段命名为&#34; user&#34;你可以给它一个人名#34;用户名&#34;。注意:如果您希望将字段名称存储在语言文件中,请参阅翻译字段名称
3.此表单字段的验证规则。
参考: https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#validationrules
谢谢
答案 1 :(得分:0)
更改
$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 :(得分:0)
这太蠢了,我忘了添加
<?php echo validation_errors(); ?>
在视图中,