Welcome Guest, Not a member yet? Register   Sign In
Form validation with array fields, error message
#1

[eluser]Treeda[/eluser]
Hi all,

i know this is an often discussed problem, and it seems the set_value etc is fixed in the most current version but i still have a problem with error message when i use arrays as field names.

having multiple fields with the same name, defining the rule this way
$this->form_validation->set_rules('motortype[]', 'xxx', 'trim|required|numeric|xss_clean');

works fine so far, even repopulation with set_value("motortype[]");

but showing errors like this
<?php echo form_error('motortype[]'); ?>

doesn't work, the error msg shows up for all fields. Does anyone have a solution for this?

Thank You
Treeda
#2

[eluser]Treeda[/eluser]
Ok i think i got a fix myself for this VERY annoying problem... trying to post the changed areas here.

Comments are welcome, hope thie helps. (It's not tested in every situation yet)

i don't know how to mark the changed segments with bold or color inside a code section but i think you will find it. just compare the code snippets against the original function of the current version 2.0.0

form_validation.php::error

Code:
...        if ($suffix == '')
        {
            $suffix = $this->_error_suffix;
        }
        
                //FIX
        // If the data is an array output them one at a time.
        //     E.g: form_input('name[]', set_value('name[]');
        if (is_array($this->_field_data[$field]['error']))
        {
        if ( $err = array_shift($this->_field_data[$field]['error']) )
            return $prefix.$err.$suffix;
        else
            return '';
        }

        return $prefix.$this->_field_data[$field]['error'].$suffix;
    }


form_validation.php::_execute


Code:
...
                // Save the error message
                //FIX
                if ($_in_array == TRUE){
                    if ( !is_array($this->_field_data[$row['field']]['error']) )
                        $this->_field_data[$row['field']]['error'] = Array();
                        
                    $this->_field_data[$row['field']]['error'][$cycles] = $message;
                }
                else
                    $this->_field_data[$row['field']]['error'] = $message;
                
                if ( ! isset($this->_error_array[$row['field']]))
                {
                    $this->_error_array[$row['field']] = $message;
                    
                    /*
                    if ($_in_array == TRUE)
                        $this->_error_array[$row['field']][$cycles] = $message;
                        */
                }

                return;
            }
            //FIX
            else
            {
                // Save the error message
                if ($_in_array == TRUE){
                    if ( !is_array($this->_field_data[$row['field']]['error']) )
                        $this->_field_data[$row['field']]['error'] = Array();
                        
                    $this->_field_data[$row['field']]['error'][$cycles] = "";
                }
            }
#3

[eluser]Treeda[/eluser]
one more addition...

i use smarty integrated into CIs view and using the above in my templates like this

Code:
<select name="motortype[]">
            <option value="">bitte wählen...</option>
            {html_options options=$motor_types selected="motortype[]"|set_value:{"motortype[]"}}
        </select>
        {"motortype[]"|form_error}
#4

[eluser]Treeda[/eluser]
Another addition and i think i give up... this really makes me sick.

Now having a problem prepopulating the fields properly

so not forwarding values but really prepopulating. so forwarding works but giving tghe default value to set_value would make it neccessary to give this an array also instead of single values.


Slowly i ask myself how would one (YOU?) do a dnymic form? In my situation i have a form of 5 fields which can be replicated multiple times 1 - x.

besides the good old way doing anything manually (if i do this why do i need CI) what other options do you have?

I'm stumbling over this problem with CI since years and it disappoints me more and more.




Theme © iAndrew 2016 - Forum software by © MyBB