我有一个1 添加用户表单,可以将多个用户添加到10个。
现在,我需要在form_validation
规则中仅验证1个字段。但是,可以肯定的是,我现在需要的是不是固定的字段要验证。因此,我可以在表单内的随机字段上提交数据。(可用的10个字段,只需要1个必需的字段,但再次不是固定字段,因为我被尝试了固定字段并真正困扰我)。
<form action="x" method="post" class="form-horizontal">
<?php for ($i=1; $i<=10; $i++) { ?>
<div class="form-group">
<label class="control-label col-sm-3">Username <?php echo $i; ?></label>
<div class="col-sm-9">
<input type="text" class="form-control" name="user[]">
</div>
</div>
<?php } ?>
Controller功能中的验证规则
if ( ! empty($this->input->post('user', TRUE)))
{
$this->form_validation->set_rules('user[]', 'User', 'trim|alpha_numeric|min_length[2]');
}
我正在尝试将required
添加到user[]
验证规则中,因为所有user[]
字段都是必需的。
然后我尝试将$this->form_validation->set_rules('user[0]', 'User', 'trim|required|alpha_numeric|min_length[2]');
添加到表单验证规则中,因此代码看起来像这样。
if ( ! empty($this->input->post('user', TRUE)))
{
$this->form_validation->set_rules('user[0]', 'User', 'trim|required|alpha_numeric|min_length[2]');
$this->form_validation->set_rules('user[]', 'User', 'trim|alpha_numeric|min_length[2]');
}
需要成功验证一个字段,即Array 0
上的Username 1
字段。 但是有时候,这确实使我只输入1个新用户,仅在Username 1
上。
有时我很容易在Username 4
或Username 9
上输入,因为光标在附近。
无论哪个字段在哪个字段中,有任何方法可以在1个字段上验证此user[]
字段。像Username 3
一样,也许在Username 7
上。
您可以使用 foreach
进行简单验证,然后为您的form_validation
创建规则。
只需从form_validation
中删除所需的所需的内容,那么您需要做的就是验证数据不是空的,您可以使用1个助手变量来执行此操作。例如,我正在使用$data = 0
。
$data = 0;
$user = $this->input->post('user', TRUE);
foreach ($user as $u) {
if ($u != '')
{
$data++
}
}
if ($data == 0) { /* then set some message to said "At least 1 data required" */ }
else
{
$this->form_validation->set_rules('user[]', 'User', 'trim|alpha_numeric|min_length[2]');
}
// then validate if form_validation run
if ($this->form_validation->run())
{
// your code..
}
这将验证如果$this->input->post('user', TRUE);
输入全部为空,则$data
仍然设置为0。如果提交用户字段而不是null,则$data
将设置为++
至1。因此,您可以在任何需要的地方输入数据可能在user 1
或user 8
或任何字段上。
$data
变为1
或更多之后,您将设置form_validation
规则的新验证,然后如果验证与form_validation
规则中的要求匹配,则您的代码将运行。
如果使用FlashData匹配form_validation
要求,则可以成功地检查数据已成功提交。如果要检查是否不匹配要求,只需使用else
。
if ($this->form_validation->run())
{
$this->session->set_flashdata('message', 'We got your submitted user : '.$data.' username');
}
简短答案,否。
查看表单验证库中的主执行函数,您将看到您确定$_POST
字段是否为数组,如果是的,则分别在数组的每个项目上运行验证函数。您可以通过编写自己的验证功能并在功能的顶部进行回声来确认这一点。它将运行每个项目的一个迭代。
话虽如此,您可以在库中/my_form_validation.php中制定这样的规则:
public function required_array($unused, $config) {
$config = explode(',', $config);
$items = $_POST[$config[0]];
$i = 0;
foreach ($items as $item) {
if (!empty($item)) {
$i++;
}
}
//echo 'num items: ' . $i;
$req = isset($config[1]) ? $config[1] : 1;
if ($i < $req) {
$this->set_message('required_array', 'Atleast ' . $req . ' {field} must be filled in.');
return false;
} else {
return true;
}
}
使用类似:$this->form_validation->set_rules('user[]', 'User', 'required_array[user, 3]');
但是,这不仅是一个黑客,如果阵列中没有项目,这将永远无法达到是一个系统文件,这是不良习惯。
这是验证的代码部分,可防止解决方案工作:
if (
($postdata === NULL OR $postdata === '')
&& $callback === FALSE
&& $callable === FALSE
&& ! in_array($rule, array('required', 'isset', 'matches'), TRUE)
)
{
continue;
}
因此,最好的选择是检查是否在CI验证的外部填写。