Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8023015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:26:17+00:00 2026-06-04T22:26:17+00:00

I have an array of profile data I need to validate: $user_group_profiles = $this->input->post(‘user_group_profiles’,

  • 0

I have an array of profile data I need to validate:

$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 option, but when no value is set for those fields, CI validation complains. 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 might be wrong?

EDIT 1:

I went into CI’s Form_validation library and made $_field_data and public member. When I var_export it after, I got this:

  '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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T22:26:19+00:00Added an answer on June 4, 2026 at 10:26 pm

    Ok – I’ve just spent an hour on this – and I’ve worked out the problem + solution

    The issue is that when the variable you are testing is part of an array, CI interprets the field as being set, and thus “contains” a value (even when it is empty). Because that “value” is NOT a number greater than 0 – it fails.

    Therefore you’ll need to unset the $_POST (NOT $user_group_profiles) variable when it is “empty” so that it passes validation. Note – validation is run on $_POST – that is why you are unsetting $_POST and not $user_group_profiles

    So the workaround is this:

    $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']);
         }
    }
    

    I’ve tested this – and it fixes your problem.

    You could also program it this way if you dont want to touch the $_POST data:

    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]");
             }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have an array that looks like this: foreach($obj as $key => $value) {
In my model named Profile_model I have this function which retrieves profile data for
I have array of objects: var aoo = [{},{},{},....{},{},{}]; I need a optimized function
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
I have array result like this: Array ( [0] => stdClass Object ( [id_global_info]
I have array of strings, String[] data and it's 10 elements has value P
I have an array of point data (for particles) which constantly changes size. To
I have successful able to save data in User and Profile model, the problem
I have a Profile form that inherits from sfGuardRegisterForm I have these fields: $this->useFields(
How do we build a profile page that outputs the user's data? and this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.