据我了解,如果验证失败,只有经过验证的值才能重新加载到表单上。目前我有以下表格
<?php echo form_label('Vendors', 'vendors'); ?>
<?php echo form_dropdown('vendors', $vendors, $this->input->post('vendors')); ?>
<br>
<?php echo form_label('Title', 'title'); ?>
<?php echo form_input('title', set_value('title')); ?>
<br><br>
<?php echo form_label('Custom Query', 'query'); ?>
<?php echo form_textarea('query', set_value('query')); ?>
<br>
<?php echo form_hidden('id', set_value('id')); ?>
<?php echo form_submit('submit', 'Submit Query') ?>
以下验证规则:
'my_rule' => [
[
'field' => 'vendors',
'label' => 'Vendors',
'rules' => 'required|greater_than[0]'
],
[
'field' => 'title',
'label' => 'Title',
'rules' => 'trim|required|xss_clean'
],
[
'field' => 'query',
'label' => 'Custom Query',
'rules' => 'trim|required|xss_clean'
]
]
但是,如果验证失败,我的隐藏字段(id)将不会重新加载值,因为它不是必需的。有没有办法绕过这个/我错过了什么?
答案 0 :(得分:1)
您的规则中必须包含“id”字段,即使不是必需的。你可以“不要求”它,只需修剪它,例如:
'my_rule' => [
[
'field' => 'vendors',
'label' => 'Vendors',
'rules' => 'required|greater_than[0]'
],
[
'field' => 'title',
'label' => 'Title',
'rules' => 'trim|required|xss_clean'
],
[
'field' => 'query',
'label' => 'Custom Query',
'rules' => 'trim|required|xss_clean'
],
[
'field' => 'id',
'label' => 'ID',
'rules' => 'trim'
]
]
像这样,该字段将在提交后重新填充,不会更改(除了修剪但我觉得在你的情况下没关系)