CodeIgniter:验证表单数组不起作用

Sta*_*bie 5 codeigniter codeigniter-2

我有一系列需要验证的配置文件数据:

$user_group_profiles = $this->input->post('user_group_profiles', TRUE);
foreach ($user_group_profiles as $key => $user_group_profile)
{
    $this->form_validation->set_rules("user_group_profiles[$key][profile_name]", 'Profile Name', 'trim|required');
    $this->form_validation->set_rules("user_group_profiles[$key][birthdate]", 'Birthdate', 'trim|required');

    // TODO: heigth/weight not required, but the validation somehow makes it required
    $this->form_validation->set_rules("user_group_profiles[$key][height]", 'Height', 'trim|greater_than[0]');
    $this->form_validation->set_rules("user_group_profiles[$key][weight]", 'Weight', 'trim|greater_than[0]');
}
Run Code Online (Sandbox Code Playgroud)

高度和重量是选项,但是当没有为这些字段设置值时,CI验证会抱怨.A var_dump($user_group_profiles);显示了这个:

array
  'ugp_b33333338' => 
    array
      'profile_name' => string '' (length=0)
      'birthdate' => string '' (length=0)
      'height' => string '' (length=0)
      'weight' => string '' (length=0)
Run Code Online (Sandbox Code Playgroud)

什么想法可能是错的?

编辑1:

我进入了CI的Form_validation库并成为了$_field_data公共成员.当我在var_export之后,我得到了这个:

  'user_group_profiles[ugp_833333338][height]' => 
    array
      'field' => string 'user_group_profiles[ugp_833333338][height]' (length=42)
      'label' => string 'Height' (length=6)
      'rules' => string 'greater_than[1]' (length=15)
      'is_array' => boolean true
      'keys' => 
        array
          0 => string 'user_group_profiles' (length=19)
          1 => string 'ugp_833333338' (length=13)
          2 => string 'height' (length=6)
      'postdata' => string '' (length=0)
      'error' => string 'The Height field must contain a number greater than 1.' (length=54)
Run Code Online (Sandbox Code Playgroud)

Lau*_*nce 7

好的 - 我刚刚花了一个小时 - 我已经解决了问题+解决方案

问题在于,当您测试的变量是数组的一部分时,CI将该字段解释为已设置,因此"包含"一个值(即使它是空的).因为"值"不是大于0的数字 - 它失败了.

因此,当它为"空"时,您需要取消设置$ _POST(NOT $ user_group_profiles)变量,以便它通过验证.注意 - 验证是在$ _POST上运行的 - 这就是为什么要取消设置$ _POST而不是$ user_group_profiles

所以解决方法是这样的:

$user_group_profiles = $this->input->post('user_group_profiles', TRUE);
foreach ($user_group_profiles as $key => $user_group_profile)
{
     // Set the rules
     $this->form_validation->set_rules($key."[profile_name]", "Profile Name", "trim|required");
     $this->form_validation->set_rules($key."[birthdate]", "Birthdate", "trim|required");
     $this->form_validation->set_rules($key."[height]", "Height", "trim|greater_than[0]");
     $this->form_validation->set_rules($key."[weight]", "Weight", "trim|greater_than[0]");

     // Now check if the field is actually empty
     if (empty($user_group_profile['height']))
     {
         // If empty, remove from array so CI validation doesnt get tricked and fail
         unset($_POST[$key]['height']);
     }
     if (empty($user_group_profile['weight']))
     {
         unset($_POST[$key]['weight']);
     }
}
Run Code Online (Sandbox Code Playgroud)

我测试了这个 - 它解决了你的问题.

如果你不想触摸$ _POST数据,你也可以这样编程:

foreach ($user_group_profiles as $key => $user_group_profile)
    {
         // Set the rules
         $this->form_validation->set_rules($key."[profile_name]", "Profile Name", "trim|required");
         $this->form_validation->set_rules($key."[birthdate]", "Birthdate", "trim|required");


         // Now check if the field is actually empty
         if ( ! empty($user_group_profile['height']))
         {
             $this->form_validation->set_rules($key."[height]", "Height", "trim|greater_than[0]");
         }
         if ( ! empty($user_group_profile['weight']))
         {
             $this->form_validation->set_rules($key."[weight]", "Weight", "trim|greater_than[0]");
         }
    }
Run Code Online (Sandbox Code Playgroud)