Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter: form validation and array bug?
#1

[eluser]CodeIgniterNewbie[/eluser]
I have an array of profile data I need to validate:
Code:
$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]');
    }

Height and weight are optional, but when no value is set for those fields, CI validation complains. A `var_dump($user_group_profiles);` shows this:
Code:
array
      'ugp_b33333338' =>
        array
          'profile_name' => string '' (length=0)
          'birthdate' => string '' (length=0)
          'height' => string '' (length=0)
          'weight' => string '' (length=0)

Any ideas what might be wrong?

EDIT 1:

**EDIT 1:**

I went into CI's Form_validation library and made $_field_data a public member. When I var_export it, I got this:
Code:
'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)

EDIT 2:

My feeling is that the problem may be connected to the fact that I'm trying to validation a form array.
#2

[eluser]Glazz[/eluser]
Try this way:

Code:
if ( $this->input->post('weight') ){
$this->form_validation->set_rules("user_group_profiles[$key][weight]", 'Weight', 'trim|greater_than[0]');
}

And do the same for Height.. this is the way i do when i need "optional" fields but i need to validate them when needed.


Edit:
Try to remove the trim first and see if it works, i know that not some time ago i had some problems with trim being a pain in the a** when i wanted to have some optional params.. but i recommend to use the first method though..
#3

[eluser]CodeIgniterNewbie[/eluser]
Removing trim did nothing. Problem also exist if the rule is "numeric", etc. I understand the if-then solution, but don't understand why I have to do this. Something must be wrong on my end.




Theme © iAndrew 2016 - Forum software by © MyBB