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

时间:2012-05-18 02:48:47

标签: 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]');
}

高度和重量是选项,但是当没有为这些字段设置值时,CI验证会抱怨。 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)

任何想法可能出错?

编辑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)

2 个答案:

答案 0 :(得分: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']);
     }
}

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

如果你不想触摸$ _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]");
         }
    }

答案 1 :(得分:1)

让我们试着把它分解成几部分。我希望我能在这里帮助你。

我假设您正在使用第二个“TRUE”参数对XSS过滤执行以下操作:

$user_group_profiles = $this->input->post('user_group_profiles', TRUE);

您实际上可以使用表单验证规则进行XSS过滤,或者如果您希望在规则之后过滤帖子。请参阅此处以了解我的偏好:

$this->form_validation->set_rules('profile_name', 'Profile Name', 'xss_clean|trim|required');

因此,了解现在,我们可以遵循其表单验证库的CI约定。在使用表单验证库之前没有必要抓取帖子,因为它会自动检测我相信POST数据无论如何都是字段名称。例如:

$this->form_validation->set_rules('profile_name', 'Profile Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('birthdate', 'Birthdate', 'trim|required|xss_clean');
$this->form_validation->set_rules('height', 'Height', 'trim|greater_than[0]|numeric');
$this->form_validation->set_rules('weight', 'Weight', 'trim|greater_than[0]|numeric');

if ($this->form_validation->run() == FALSE) {
    $this->load->view('my_view_where_this_post_came_from');
} else {
    $profile_name = $this->input->post('profile_name');

    //or if you prefer the XSS check here, this:
    //$profile_name = $this->input->post('profile_name', TRUE);

    $birthdate= $this->input->post('birthdate');
    $height= $this->input->post('height');
    $weight= $this->input->post('weight');

    //$user_group_profiles = $this->input->post();

}

我希望这有帮助!

编辑:我也注意到了这一点。如果您尝试获取整个帖子数组,则代码为:

$user_group_profiles = $this->input->post(NULL, TRUE); // returns all POST items with XSS filter 
$user_group_profiles = $this->input->post(); // returns all POST items without XSS filter

 $user_group_profiles = $this->input->post('user_group_profiles');

如果您不知道$ _POST名称或者感到困惑,那么这是一个很好的帮助,您可以这样做以查看该数据是否存在!像这样作为你的第一行:

var_dump($_POST);
exit();