如何在CodeIgniter表单验证中的10个字段中仅验证1个array []

时间:2017-12-09 11:51:42

标签: php codeigniter

我有一个1 添加用户表单,可以添加多达10个用户。

现在我需要验证https://<appname>.scm.azurewebsites.net/api/logstream规则中只需要1个字段。但是,当然我现在需要的是不是固定字段来验证。所以,我可以在表单内的randomize字段中提交数据。 (10个可用字段,只需要1个必填字段,但再次不是固定字段因为我尝试了固定字段并且真的困扰我)。

form_validation

控制器功能内的验证规则

<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 } ?>

我尝试将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]'); } 字段Username 1字段。  但有时我只在0上输入一个新用户真的很困扰。

因为有时我觉得在Username 1Username 4上输入非常容易,因为光标就在附近。

是否有任何方法可以验证此Username 9字段仅适用于1字段,无论哪个字段存在。就像user[]上的Username 3一样。

2 个答案:

答案 0 :(得分:1)

在为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.如果提交的用户字段不为空,则$data将设置为++ {1}}至1.因此,您可以在user 1user 8或任何字段中输入您想要的数据。

$data变为1或更多后,您将设置form_validation规则的新验证,如果验证符合{{1}中的要求,您的代码将会运行规则。

您可以使用flashdata检查数据是否已成功提交如果 form_validation要求匹配。如果您想检查要求是否不匹配,只需使用form_validation

else

答案 1 :(得分:0)

简短回答,不。

查看表单验证库中的主执行函数,您将看到确定$_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]');

然而,这不仅仅是一个黑客攻击,如果数组中没有项目,因为CI有一种特殊的处理方式要求我们不能修改或添加我们的功能,因为它是一个系统文件,这是不好的做法。

以下是验证中阻止解决方案工作的代码部分:

    if (
        ($postdata === NULL OR $postdata === '')
        && $callback === FALSE
        && $callable === FALSE
        && ! in_array($rule, array('required', 'isset', 'matches'), TRUE)
    )
    {
        continue;
    }

因此,最好的办法是检查是否在CI验证之外填写了一个。