CodeIgniter Forums
CI 1.7svn array form_validation problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: CI 1.7svn array form_validation problem (/showthread.php?tid=12212)



CI 1.7svn array form_validation problem - El Forum - 10-10-2008

[eluser]CI shocki[/eluser]
Hi guys,

I have the following validation config:

Code:
array(
   'field' => 'description_teaser[]',
   'label' => '',
   'rules' => ''
),...

in my view I have
Code:
<textarea name="description_teaser[]" id="elm1"><?php echo set_value('description_teaser[]'); ?></textarea>

When repopulating the form this does not work. Instead of the expected result every textarea field ist filled with the text "array"

Any ideas?

Regards shocki


CI 1.7svn array form_validation problem - El Forum - 10-10-2008

[eluser]xwero[/eluser]
IMO If you want to repopulate fields that are a part of an array you should specify the key(s).
Let's say you do something like below in your view
Code:
<?php foreach($fields as $field): ?>
<p>&lt;input type="text" name="text[]" value="&lt;?php echo set_value('text[]'); ?&gt;"&gt;&lt;/p>&lt;?php endforeach ?&gt;
The library would have to have an internal counter to know which value is added to which field. But what if the input tag is an array of checkboxes. If a checkbox isn't checked it isn't added to the POST global.

I think if you are going to use arrays you shouldn't expect the library to almost read your mind. It's possible but i think the performance hit will be significant.


CI 1.7svn array form_validation problem - El Forum - 10-10-2008

[eluser]CI shocki[/eluser]
something like that is working.

Code:
&lt;?php $var=set_value('description_teaser[]'); echo is_array(description_teaser[]) ? $var[0] : ''; ?&gt;

But to me this solution doesn't make that much sense.

Rick, Derek? Isn't that meant to be an automatic approach?


CI 1.7svn array form_validation problem - El Forum - 10-10-2008

[eluser]xwero[/eluser]
This is the library's set_value code
Code:
/**
     * Get the value from a form
     *
     * Permits you to repopulate a form field with the value it was submitted
     * with, or, if that value doesn't exist, with the default
     *
     * @access    public
     * @param    string    the field name
     * @param    string
     * @return    void
     */
    function set_value($field = '', $default = '')
    {
        if ( ! isset($this->_field_data[$field]))
        {
            return $default;
        }

        return $this->_field_data[$field]['postdata'];
    }
As you see it's a very simple function. The field value is put in the postdata part of the fields array as an array so that is why you got the array value.


CI 1.7svn array form_validation problem - El Forum - 10-10-2008

[eluser]CI shocki[/eluser]
Yeah it seems like it.

This is from the manual:

Quote:Or to re-populate the field you would use:
Code:
&lt;input type="text" name="options[]" value="&lt;?php echo set_value('options[]'); ?&gt;" size="50" /&gt;

This would always output "array". So this example doesn't really make sense.


CI 1.7svn array form_validation problem - El Forum - 10-10-2008

[eluser]xwero[/eluser]
The array handling of the library has some rough edges that need to be polished before it gets released. But if you have a good idea how to fix it join the thread on the Form_validation library started by Rick.