Custom Formfieldlist with multiple values only selects the first value

165 views
Skip to first unread message

Juan Diego

unread,
Apr 25, 2012, 8:03:13 PM4/25/12
to joomla-de...@googlegroups.com
Hi
I created a fieldlist class that reads some values from a googlefusiontable, it basically has the categories stored on the google fusion tables, and the value in the database is stored in a string.  So for example, if I have selected 3 categories, everything is stored in one string like this: category1, category4, category6.

This is the code of the class

class JFormFieldFusionCategories extends JFormFieldList {
 
    //The field class must know its own type through the variable $type.
    protected $type = 'fusioncategories';
 
    public function getOptions() {
        // Initialize variables.
        $options = array();
       
        // Initialize some field attributes.
        $key = $this->element['key_field'] ? (string) $this->element['key_field'] : 'value';
        $value = $this->element['value_field'] ? (string) $this->element['value_field'] : (string) $this->element['name'];
        $translate = $this->element['translate'] ? (string) $this->element['translate'] : false;
        $query = (string) $this->element['query'];
       
        $params=JComponentHelper::getParams('com_mapa');
        //var_dump($params);
        $guser = $params->get('googleuser');
        $gpass = $params->get('googlepass');
        $gtable = $params->get('googletablecategories');
        //We connect to google
        $token = ClientLogin::getAuthToken($guser, $gpass);
        $ftclient = new FTClientLogin($token);
       
        $query=$ftclient->query(SQLBuilder::select($gtable, array('Categoria')));
        $categories = explode("\n",$query);
        $selected=null;
       
        foreach($categories as $key => $category)
        {
            if($key!=0 && $category[0]){
                //$category=str_getcsv($category,",");
                $options[] = JHtml::_('select.option', $category);
            }
        }

        // Merge any additional options in the XML definition.
        $options = array_merge(parent::getOptions(), $options);
       
        return $options;
    }
    protected function getInput()
    {
        // Initialize variables.
        $html = array();
        $attr = '';
       
        // Initialize some field attributes.
        $attr .= $this->element['class'] ? ' class="' . (string) $this->element['class'] . '"' : '';

        // To avoid user's confusion, readonly="true" should imply disabled="true".
        if ((string) $this->element['readonly'] == 'true' || (string) $this->element['disabled'] == 'true')
        {
            $attr .= ' disabled="disabled"';
        }

        $attr .= $this->element['size'] ? ' size="' . (int) $this->element['size'] . '"' : '';
        $attr .= $this->multiple ? ' multiple="multiple"' : '';

        // Initialize JavaScript field attributes.
        $attr .= $this->element['onchange'] ? ' onchange="' . (string) $this->element['onchange'] . '"' : '';

        // Get the field options.
        $options = (array) $this->getOptions();

        // Create a read-only list (no name) with a hidden input to store the value.
        if ((string) $this->element['readonly'] == 'true')
        {
            $html[] = JHtml::_('select.genericlist', $options, '', trim($attr), 'value', 'text', $this->value, $this->id);
            $html[] = '<input type="hidden" name="' . $this->name . '" value="' . $this->value . '"/>';
        }
        // Create a regular list.
        else
        {
            $values = str_getcsv($this->value,",");
            var_dump($values);
            $html[] = JHtml::_('select.genericlist', $options, $this->name, trim($attr), 'value', 'text', $values, $this->id);
        }

        return implode($html);
    }
}


As you can see I have a var_dump on $values, and it shows me something like this array(3) { [0]=> string(10) "Category3" [1]=> string(7) " Category6" [2]=> string(9) " Category7" }, but the only category  that is selected is the first one of the array. Am I passing the values in wrong way?

Sam Coult

unread,
Apr 26, 2012, 4:36:30 PM4/26/12
to joomla-de...@googlegroups.com

Hi Juan,

When I have had this issue before it has mostly been because the value is not being saved into the database.  If you are specifying multiple="multiple" then you should read through this recent thread which discusses the difficulties in saving multiple fields (checkboxes, multiselect lists etc.), not that it is really incredibly difficult but you will need to add a save() function to your JModelAdmin model and use implode to convert the array for that field into a string.  Then in your loadFormData function you need to remember to explode the values so that they can be loaded back into the list.

Hope that helps.

Matias Aguirre

unread,
Apr 26, 2012, 11:08:28 PM4/26/12
to joomla-de...@googlegroups.com

I used multiple="true" option exactly how Sam says but instead of add a save() function on the model i only added a bind() function in my table class:

    public function bind($src, $ignore = array())
    {
        // Fix the multiple ids data
        if (is_array($src['catid'])) {
            $src['catid'] = implode(",", $src['catid']);
        }

        return parent::bind($src);
    }

I think that is the same thing
Reply all
Reply to author
Forward
0 new messages