Multi-select (type = array) and fieldsets

edited May 2012 in Feature Requests
Given this example inside my model's form_fields() declaration:

$fields['related_entries']= array(
'type' => 'array',
'mode' => 'multi',
'options' => array(1 => 'Test one', 2 => 'Test Two', 3 => 'Test Three'),
'value' => array(2, 3)
);
how can i have options separated by 'fieldsets' ?
I mean that in the multi-select I'd love to have entries separated by letter for example, or stuff like that.

I've tried the following
$fields['related_entries']= array(
'type' => 'array',
'mode' => 'multi',
'options' => array(
'A' => array(1 => 'Test one', 2 => 'Test Two', 3 => 'Test Three'),
'B' => array(1 => 'Test one', 2 => 'Test Two', 3 => 'Test Three')
),
'value' => array(2, 3)
);
but that won't work... any clues?
thanks!

UPDATE: i've just realized that the 2nd example actually outputs the 'optgroups' to the HTML, but the multi select won't render them for some reason... any ideas?

Comments

  • edited 1:17PM
    Unfortunately, the jquery widget used to render that doesn't support optgroups.
  • edited 1:17PM
    thanks for clarifying!
  • edited January 2021

    Hi both, I am having problem storing multi field into database. I am getting error: Message: Array to string conversion

    `

            $options = array('a' => 'option A', 'b' => 'option B', 'c' => 'option C');          
            $fields['odobreni_proizvodjaci'] = array('label' => 'Available options:', 'type' => 'multi', 'mode' => 'multi', 'options' => $options);
    

    `

    I would like to save it as JSON if possible. And how can I than Edit this field afterwards if needed?

    Thanks!

  • Update: I have managed to save data as JSON by using this below in on_before_save

            if (!empty($values['odobreni_proizvodjaci'])) 
        {
            $priprema = $values['odobreni_proizvodjaci'];
            $pretvori = json_encode($priprema);
    
            $values['odobreni_proizvodjaci'] = $pretvori;
        }
    

    Now just have to reverse the process on editing, populate multi field back. If you can help with that :) Thanks

  • I think what you want is the serialized_fields on your model:
    https://docs.getfuelcms.com/libraries/my_model

    This is a property on the model that you can specify one or more fields in your model (in an array) that get serialized and deserialized upon retrieval. The default serialization method is JSON.

  • edited January 2021

    That actually sound good. What I am doing is fetching data with query form one of the tables and populating multi list. I am basically extending Fuel_users_model with few more fields.

    Function inside Fuel_users_model.php:

        public function vratiDobavljace($limit = NULL, $offset = NULL, $col = 'NazivProizvodjaca', $order = 'asc', $just_count = FALSE)
        {
            $CI =& get_instance();
            $this->db->select('NazivProizvodjaca, Proizvodjac');
            $this->db->where('Proizvodjac !=', 0); // gdje je proizvođač različit od nule
            $this->db->group_by("NazivProizvodjaca");
            $this->db->order_by('NazivProizvodjaca ASC');
            $this->db->from('proizvodjaci');
            //$query = $this->db->get();
            $data = parent::list_items($limit, $offset, $col, $order, $just_count);
    
            $mojArray = array( 000 => "------- Odaberite dobavljača -------" );
    
            //$arr = array(35=>"Novi Travnik", 37=>"Sarajevo", 43=>"Mostar");
            foreach ($data as $row)
            {    
                $mojArray += [ $row["Proizvodjac"] => $row["Proizvodjac"]." - ".$row["NazivProizvodjaca"] ];
            }
    
    
            return $mojArray;
        }
    

    Than inside public function form_fields I am using this below:

      $opcije = $this->vratiDobavljace();
      $fields['odaberi_dobavljaca'] = array('label' => 'Brzi odabir dobavljača', 'type' => 'select2', 'size' => 80, 'order' => 9, 'options' => $opcije);
    

    And inside on_before_save($values) function I use this below to prepare data:

         if (!empty($values['odobreni_proizvodjaci'])) 
        {
            $priprema = $values['odobreni_proizvodjaci'];
            $pretvori = json_encode($priprema, JSON_FORCE_OBJECT);
    
            $values['odobreni_proizvodjaci'] = $pretvori;
        }
         return $values;
    

    This saves data into DB and that is fine. Last thing to figure out is how to (on user edit) retrive saved multi field data and display it in the right pane where selected items are.

    I don't really know how to use serialized_fields.

  • Thanks, serialized_fields sorted things. It's super easy to use it. Thanks a lot

Sign In or Register to comment.