CodeIgniter: form array validation does not work

I have an array of profile data that needs to be checked:

$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 an option, but when no value is specified for these fields, CI authentication is checked. A var_dump($user_group_profiles);shows this:

array
  'ugp_b33333338' => 
    array
      'profile_name' => string '' (length=0)
      'birthdate' => string '' (length=0)
      'height' => string '' (length=0)
      'weight' => string '' (length=0)

Any ideas what could be wrong?

EDIT 1:

I went to the CI Form_validation library and made a $_field_datapublic member as well. When I var_export it, I got the following:

  '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)
+5
source share
2 answers

Ok - I spent an hour on this - and I developed a problem + solution

, , , , 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]");
         }
    }
+7

. , .

, XSS "TRUE":

$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();

}

, !

EDIT: . , :

$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();
+1

All Articles