Welcome Guest, Not a member yet? Register   Sign In
Function set_value() not working as expected
#11

[eluser]cwt137[/eluser]
[quote author="ifew" date="1234095714"]hello
patch from cwt137
i can't use this, it return first field value to every input[/quote]
Can you post a test case where my patch doesn't work? Then I can see what I can do to fix it.
#12

[eluser]ifew[/eluser]
[quote author="cwt137" date="1234131367"][quote author="ifew" date="1234095714"]hello
patch from cwt137
i can't use this, it return first field value to every input[/quote]
Can you post a test case where my patch doesn't work? Then I can see what I can do to fix it.[/quote]


in view
Code:
<input type=‘text’ name=“optoin[]” size=“30” value=”<?=set_value(‘optoin[]’); ?>”/>
<input type=‘text’ name=“optoin[]” size=“30” value=”<?=set_value(‘optoin[]’); ?>”/>
<input type=‘text’ name=“optoin[]” size=“30” value=”<?=set_value(‘optoin[]’); ?>”/>
and set in rule

in rule config
Code:
array(
  ‘field’ => ‘optoin[]’,
  ‘label’ => ‘lang:user_option’,
  ‘rules’ => ‘trim|htmlspecialchars|xss_clean’
)

it's return value from first field to all
but if use indexing number to optoin[] field and rule. (such as optoin[0],optoin[1],..)
it's ok


ps. sorry i'm bad english
#13

[eluser]kandmcreative[/eluser]
I think we definitely need a solution where the array index is not needed, since often times, we don't know how many elements we will have.
#14

[eluser]Helmet[/eluser]
Please fix this. It drives me bananas to be forced to put worthless lines like:

$this->form_validation->set_rules('field','field name','');

into my code just so that I can repopulate my field.
#15

[eluser]gigas10[/eluser]
[quote author="kandmcreative" date="1235519540"]I think we definitely need a solution where the array index is not needed, since often times, we don't know how many elements we will have.[/quote]

ever heard of a loop?
#16

[eluser]tison[/eluser]
Is there a fix for this - without adding an empty rule? Subscribing...
#17

[eluser]tison[/eluser]
For a quick fix, I've created this function to easily set empty rules - so that all fields get re-populated:

In /helpers/MY_form_helper.php

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Set empties
*
* Sets empty fields using set_value, hopefully they will fix this
*
*/
if ( ! function_exists('set_empty_rules'))
{
    function set_empty_rules($field_array)
    {
        if(isset($field_array) && is_array($field_array)):
            $mythis =& get_instance(); // grabbing our ci this instance
            foreach($field_array as $key => $val):
                $mythis->form_validation->set_rules($key,$val,'');
            endforeach;
        endif;
    }
}

And in your controller:

Code:
set_empty_rules(array('address'=>'Address','city'=>'City','state'=>'State','zip'=>'Zip'));
The function takes an array of key -> values where key = 'fieldname' and value='Field Error Message'
#18

[eluser]Tooker[/eluser]
Had this same problem using 1.7.2 and google took me here. This is still unresolved. Regards,

Rob
#19

[eluser]jbreitweiser[/eluser]
I looked through the code and the problem is the the set_value() function will call the set_value function in the form_validation class. The form validation class only has the post data for the fields set up with a form validation. This needs to happen because during validation a callback function can return a new value instead of true or false.

All of the set_value functions need to check if there is a form validation defined, and if so use the input->post value. If I get something to work I will post some code.
#20

[eluser]jbreitweiser[/eluser]
Fixed extra space in last return statement.

Replace the set_value, set_select, set_radio, and set_checkbox in the /system/libraries/Form_validation.php file. Basically if the form validator does not have the value, the code checks the input class.

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]))
        {
            if( $this->CI->input->post($field)===FALSE)
            {
                return $default;
            }
            else
            {
                return $this->CI->input->post($field);
            }
        }
        
        return $this->_field_data[$field]['postdata'];
    }
    
    // --------------------------------------------------------------------
    
    /**
     * Set Select
     *
     * Enables pull-down lists to be set to the value the user
     * selected in the event of an error
     *
     * @access    public
     * @param    string
     * @param    string
     * @return    string
     */    
    function set_select($field = '', $value = '', $default = FALSE)
    {        
        return $this->set_value_array($field, $value, ' selected="selected"', $default);
    }
    
    // --------------------------------------------------------------------
    
    /**
     * Set Radio
     *
     * Enables radio buttons to be set to the value the user
     * selected in the event of an error
     *
     * @access    public
     * @param    string
     * @param    string
     * @return    string
     */    
    function set_radio($field = '', $value = '', $default = FALSE)
    {
        return $this->set_value_array($field, $value, ' selected="selected"', $default);
    }
    
    // --------------------------------------------------------------------
    
    /**
     * Set Checkbox
     *
     * Enables checkboxes to be set to the value the user
     * selected in the event of an error
     *
     * @access    public
     * @param    string
     * @param    string
     * @return    string
     */    
    function set_checkbox($field = '', $value = '', $default = FALSE)
    {
        return $this->set_value_array($field, $value, ' checked="checked"', $default);
    }
    
    function set_value_array($field = '', $value = '', $default_value = '' ,$default = FALSE){
        if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
        {
            if( ! ($this->CI->input->post($field) === FALSE))
            {
                $field = $this->CI->input->post($field);
            }
            else
            {
                if ($default === TRUE AND count($this->_field_data) === 0)
                {
                    return $default_value;
                }
                return '';
            }
        }
        else
        {
        $field = $this->_field_data[$field]['postdata'];
        }
        
        if (is_array($field))
        {
            if ( ! in_array($value, $field))
            {
                return '';
            }
        }
        else
        {
            if (($field == '' OR $value == '') OR ($field != $value))
            {
                return '';
            }
        }
            
        return $default_value;
    }




Theme © iAndrew 2016 - Forum software by © MyBB