【问题标题】:How can I use CodeIgniter's set_value for a field that's an array?如何将 CodeIgniter 的 set_value 用于数组字段?
【发布时间】:2010-06-18 17:18:51
【问题描述】:

我有一个标有“amenities[]”的下拉菜单,它是一个数组。当我使用 CodeIgniter 的 form_validation 时,我想使用 set_value 正确地重新填充它,但我做不到。有人对此有什么意见吗?

【问题讨论】:

    标签: codeigniter validation


    【解决方案1】:

    查看 Codeignitor 1.7.2 的源代码,set_value 实现是:

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

    注意它不支持数组。另一方面,Codeignitor 的 set_select 确实支持数组:

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

    【讨论】:

    • 非常感谢!这条评论Permits you to repopulate a form field with the value it was submitted * with, or, if that value doesn't exist, with the default 确实是我想要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多