• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
set_value() and input names that are arrays.

#1
[eluser]skunkbad[/eluser]
I find it odd that the set_value method that is in the Form validation library uses array_shift() to return the first value in the array, and any other elements of the array are unusable. What is the reason for this?

I extended the Form validation library to suit my needs, but it seems odd that there isn't an easier way to validate AND REPOPULATE checkboxes. It took quite a bit of work for me to get the validation of a group of checkboxes working.

view:

Code:
<div class="form-row">

    &lt;?php
        /*
         * GET VALUE OF CHECKBOXES GROUP
         */
        $submission_values = set_value('checkboxes[]', ( isset($personal_data->checkboxes) ) ? unserialize($personal_data->checkboxes) : array() );

        $checkbox_data = array(
            'name'        => 'checkboxes[]',
            'id'        => 'checkbox_one',
            'value'        => 'one',
            'checked'    => ( ! empty( $submission_values ) ) ? ((in_array('one', $submission_values)) ? 'checked' : '') : '',
            'class'        => 'form_checkbox'
        );

        echo form_checkbox($checkbox_data);
        echo form_label('One', 'checkbox_one', array('class'=>'checkbox_label'));
    ?&gt;

</div>
<div class="form-row">

    &lt;?php
        $checkbox_data = array(
            'name'        => 'checkboxes[]',
            'id'        => 'checkbox_two',
            'value'        => 'two',
            'checked'    => ( ! empty( $submission_values ) ) ? ((in_array('two', $submission_values)) ? 'checked' : '') : '',
            'class'        => 'form_checkbox'
        );

        echo form_checkbox($checkbox_data);
        echo form_label('Two', 'checkbox_two', array('class'=>'checkbox_label'));
    ?&gt;

</div>

MY_Form_validation.php:

Code:
&lt;?php

class MY_Form_validation extends CI_Form_validation {

    // need arrays to stay intact!
    function set_value($field = '', $default = '')
    {
        if ( ! isset($this->_field_data[$field]))
        {
            return $default;
        }

        return $this->_field_data[$field]['postdata'];
    }

}

What am I doing wrong? Why is it so hard to achieve checkbox validation and repopulation? Maybe it is because I am removing data from $this->form_validation->_field_data if it didn't pass validation:

Code:
// do not repopulate with data that did not validate
foreach( $_POST as $k => $v )
{
    if( array_key_exists( $k, $this->form_validation->_error_array ))
    {
            //kill set_value()
            unset($this->form_validation->_field_data[$k]);
    }
}

There's a lot to be desired...

#2
[eluser]Karman de Lange[/eluser]
Hi,
Thanks for posting this, helped me come up with stupid solution for re population of tick boxes:

Her my code:
$member_disciplines comes from a DB query by default.
Sure the much cleaner code, but this working for now.

$b=''; //clear var
while($a = set_value("member_disciplines[]")){ //while something in array, assign '1' it to b[key]

$b[$a] = 1;
};
if ($b)
$member_disciplines =$b; //if $b then replace DB variable




echo "<ul>";
foreach ($disciplines as $key=>$dis) {
echo "<li>";
echo form_label($dis);

echo form_checkbox("member_disciplines[$key]",$key,element($key,$member_disciplines));
echo "</li>";
}

echo "</ul>";
echo "</ul>";

#3
[eluser]aquary[/eluser]
EDIT: Just realized the topic was almost a year...10 minutes waste LOL

I'll left the comment here anyway .-.


Cannot remember what is exactly inside the set_value.... but,

Code:
I find it odd that the set_value method that is in the Form validation library uses array_shift() to return the first value in the array, and any other elements of the array are unusable. What is the reason for this?

It's not unsable, all the remains data is still there inside the function (what did PHP call.... static, maybe?). So the next time set_value() or whatever got called, the next value will be fetched out.

Also, you are using an odd way to repopulate the checkboxes, especially the checked part. You know you could use set_checkbox() ?

Code:
// As in the userguide
&lt;input type="checkbox" name="mycheck[]" value="1" &lt;?php echo set_checkbox('mycheck[]', '1'); ?&gt; /&gt;
&lt;input type="checkbox" name="mycheck[]" value="2" &lt;?php echo set_checkbox('mycheck[]', '2'); ?&gt; /&gt;

// in your case... , never used the form_checkbox before, but let see.
&lt;input type="checkbox" name="checkboxes" id="checkbox_one" value="one" &lt;?=set_checkbox('checkboxes[]', 'one');?&gt;
?&gt;

And the first part of your code, I assume it's data from database or so, for editting. Now trying to put a default value...
Code:
&lt;?
  // no need to call set_value here
  $values=isset($personal_data-&gt;checkboxes)?unserialize($personal_data->checkboxes) : array();
  $checkbox_data = array(
    'name'        => 'checkboxes[]',
    'id'        => 'checkbox_one',
    'value'        => 'one',
    // Here, the checked required TRUE/FALSE, not string of checked
    'checked'    => in_array('one', $submission_values),
    'class'        => 'form_checkbox'
  );
   echo form_checkbox($checkbox_data);
?&gt;

Try it, let see what will you got.

One more thing, what was the validation rules you set for the checkboxes?

Lastly, before trying, please revert/removed the changes stuff you did in the last 2 code box, to be sure are using the same script with the userguide. Smile


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.