Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok - I've just spent an hour on this - and I've worked out the problem + solution</p> <p>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.</p> <p>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</p> <p>So the workaround is this:</p> <pre><code>$user_group_profiles = $this-&gt;input-&gt;post('user_group_profiles', TRUE); foreach ($user_group_profiles as $key =&gt; $user_group_profile) { // Set the rules $this-&gt;form_validation-&gt;set_rules($key."[profile_name]", "Profile Name", "trim|required"); $this-&gt;form_validation-&gt;set_rules($key."[birthdate]", "Birthdate", "trim|required"); $this-&gt;form_validation-&gt;set_rules($key."[height]", "Height", "trim|greater_than[0]"); $this-&gt;form_validation-&gt;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']); } } </code></pre> <p>I've tested this - and it fixes your problem.</p> <p>You could also program it this way if you dont want to touch the $_POST data:</p> <pre><code>foreach ($user_group_profiles as $key =&gt; $user_group_profile) { // Set the rules $this-&gt;form_validation-&gt;set_rules($key."[profile_name]", "Profile Name", "trim|required"); $this-&gt;form_validation-&gt;set_rules($key."[birthdate]", "Birthdate", "trim|required"); // Now check if the field is actually empty if ( ! empty($user_group_profile['height'])) { $this-&gt;form_validation-&gt;set_rules($key."[height]", "Height", "trim|greater_than[0]"); } if ( ! empty($user_group_profile['weight'])) { $this-&gt;form_validation-&gt;set_rules($key."[weight]", "Weight", "trim|greater_than[0]"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload