我有一组需要验证的配置文件数据:
$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 验证会报错。Avar_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)