How can I use CodeIgniter's set_value for a field that's an array?

后端 未结 1 1399
逝去的感伤
逝去的感伤 2021-01-26 16:13

I have a dropdown labeled \"amenities[]\" and it\'s an array. When I use CodeIgniter\'s form_validation, I want to re-populate it properly using set_value, but I\'m not able to.

相关标签:
1条回答
  • 2021-01-26 17:07

    Looking at the source for Codeignitor 1.7.2 the set_value implementation is:

    /**
     * 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'];
    }
    

    Note it does not support arrays. On the other hand Codeignitor's set_select does indeed support arrays:

    // --------------------------------------------------------------------
    
    /**
     * 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)
    {       
        if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
        {
            if ($default === TRUE AND count($this->_field_data) === 0)
            {
                return ' selected="selected"';
            }
            return '';
        }
    
        $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 ' selected="selected"';
    }
    
    0 讨论(0)
提交回复
热议问题