Welcome Guest, Not a member yet? Register   Sign In
set_value for field with array name SOLVED
#1

[eluser]megabyte[/eluser]
K, first of all I have tested this but give no guarantees. I consider myself intermediate not an expert.

Second of all, I haven't tested the helper function, and I'm pretty sure it will need to be changed as well. So if someone has the time, if not I'll get to it eventually.

Anyway here ya go!

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 = '')
    {      
        // Are we dealing with an array?
        if (strpos($field, '[') !== FALSE AND preg_match('/\[(.*?)\]/', $field, $matches))
        {        
           // break it up so we can get the field name
           $x = explode('[', $field);

            // get field name
            $field = $x[0];
            
            // capture current array key => value
            $temp = each($_POST[$field]);
                    
            if ( ! isset($temp['value']))
            {
                return $default;
            }
            
            return $temp['value'];
        }
        else
        {
            // default set value
            if ( ! isset($this->_field_data[$field]))
            {
                return $default;
            }
        
            return $this->_field_data[$field]['postdata'];

        }    
    }

// Example Usage

$field = array(
    'name'    => 'field_name[]',
    'value'    => set_value('field_name[]')
);

echo form_input($field);

It be nice if some of the CI/ PHP experts could take a look, and let me know if it works for them. And if so who knows they might add it to the core
#2

[eluser]megabyte[/eluser]
found a better solution from another post.

http://ellislab.com/forums/viewthread/115253/#735080


here's the form helper set_value to go with it.

Code:
/**
* Form Value
*
* Grabs a value from the POST array for the specified field so you can
* re-populate an input field or textarea.  If Form Validation
* is active it retrieves the info from the validation class
*
* @access    public
* @param    string
* @return    mixed
*/

function set_value($field = '', $default = '')
{
    if (FALSE === ($OBJ =& _get_validation_object()))
    {
        $CI =& get_instance();
        
        if(is_array($field))
        {
            return form_prep( ($CI->_reduce_array($_POST, $field)) ? $CI->_reduce_array($_POST, $field) : $default);
        }
        if ( ! isset($_POST[$field]))
        {
            return $default;
        }

        return form_prep($_POST[$field], $field);
    }
        
    if(is_array($field))
    {
        return form_prep( ($OBJ->_reduce_array($_POST, $field)) ? $OBJ->_reduce_array($_POST, $field) : $default);
    }
    return form_prep($OBJ->set_value($field, $default), $field);
}
#3

[eluser]bkirkman[/eluser]
I'd like to chime in and mention I was facing the same issue. I couldn't get the above to work, but I found the following bug report that solved the issue for me.

Arrays as field names not working

Thanks, zdknudsen! Worked like a charm. I didn't have anything to do with it, but maybe this will help someone save some time and frustration in the future.
#4

[eluser]webdevguy[/eluser]
[quote author="bkirkman" date="1280802808"]I'd like to chime in and mention I was facing the same issue. I couldn't get the above to work, but I found the following bug report that solved the issue for me.

Arrays as field names not working

Thanks, zdknudsen! Worked like a charm. I didn't have anything to do with it, but maybe this will help someone save some time and frustration in the future.[/quote]

That bug tracker link is dead but bkirkman placed the fix on this thread: http://ellislab.com/forums/viewthread/156497/




Theme © iAndrew 2016 - Forum software by © MyBB