• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CI 1.7svn array form_validation problem

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

I have the following validation config:

   'field' => 'description_teaser[]',
   'label' => '',
   'rules' => ''

in my view I have
<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

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

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

&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?

This is the library's set_value 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.

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

This is from the manual:

Quote:Or to re-populate the field you would use:
&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.

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.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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