Welcome Guest, Not a member yet? Register   Sign In
Problem Using Arrays as Field Names (Form Validation)
#1

[eluser]EEssam[/eluser]
Hello,

I'm using Form Validation, however, I'm not getting my fields repopulated with the correct details, I'm getting the fields populated with "Array" word.

My View:

Code:
<input type="text" name="colors[]" value="<?= set_value('colors[]') ?>" size="3" />

<input type="text" name="colors[]" value="<?= set_value('colors[]') ?>" size="3" />

My Controller:

Code:
$this->form_validation->set_rules('colors[]', 'Colors', 'required');

if ($this->form_validation->run() == FALSE)
{...

What is wrong?

Please help. Thanks.
#2

[eluser]EEssam[/eluser]
I still have this problem Sad
#3

[eluser]xwero[/eluser]
try
Code:
<input type="text" name="colors[0]" value="<?= set_value('colors[0]') ?>" size="3" />

<input type="text" name="colors[1]" value="<?= set_value('colors[1]') ?>" size="3" />
#4

[eluser]Unknown[/eluser]
I can't seem to get this to work either. Tried using the method of using an index number in the field name, but still no luck. Any other suggestions?
#5

[eluser]RS71[/eluser]
try doing something like

Code:
$values_array = $this->input->post('colors');

echo $values_array[0];

note that there isn't any brackets in colors.

there might be a simpler way but I think that works (also I'm not sure if you have to have a validation rule to use $this->input->post(). )

I'm having problems with this myself
#6

[eluser]RS71[/eluser]
Actually, I have a question of my own concerning this:

I have a bunch of drop downs set as an array. When I want to repopulate the data, it sees the array instead and turns the original drop down to a multi select. How can I make it so that I can set_value of a certain key of the array or something?

Code:
<?php echo form_dropdown($field, $options, set_value($field, (isset($databasevalue->fetch)) ? $databasevalue->fetch : ''), $params); ?>
#7

[eluser]Creative Stasis[/eluser]
RS71 - Try to keep on topic here. You'll have better luck posting your own thread for that question or researching to see if it's been answered before.

I also am having the issue of the set_value populating as "ARRAY".

Does anyone have a solution for this?
#8

[eluser]kandmcreative[/eluser]
I am having the same issue with using arrays as field names in Form Validation. It seems that this documentation [Form Validation : Using Arrays as Field Names] is flawed. My workaround is to just use the $_POST array, but I can't access the error messages which defeats the purpose of the validation.

Is anyone else having this issue?
#9

[eluser]kandmcreative[/eluser]
This was my solution:

I rewrote the set_value function and put it in MY_form_validation.php to not overwrite the official version.

It never worked as written in the documentation. I think this solves the issue and matches the documentation version.

Code:
function set_value($field = '', $default = '')
    {
        if ( ! isset($this->_field_data[$field]))
        {
            return $default;
        }
        if($this->_field_data[$field]['is_array'] == TRUE){
            if(!(isset($this->_field_data[$field]['next']))){
                $this->_field_data[$field]['next'] = 0;
            }
            $next = $this->_field_data[$field]['next'];
            $this->_field_data[$field]['next']++;
            return $this->_field_data[$field]['postdata'][$next];
            
        }
        else{
            return $this->_field_data[$field]['postdata'];
        }
    }




Theme © iAndrew 2016 - Forum software by © MyBB