Welcome Guest, Not a member yet? Register   Sign In
CI 1.7svn array form_validation problem
#1

[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
#2

[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.
#3

[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?
#4

[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.
#5

[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.
#6

[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.




Theme © iAndrew 2016 - Forum software by © MyBB