public function addOptionsWithGroups($optionGroups = [], $selectedValue = '')
    {
        // Create all options
        foreach ($optionGroups as $groupLabel => $options) {
            // Create option group
            $og = $this->getHTMLFormFactory()->buildFormElement('optgroup', $name = '', $value = '', $id = '', $class = '', $itemValue = '');
            $og->attr('label', $groupLabel);

            // Create all options
            foreach ($options as $value => $title) {
                // Create option
                $fi = $this->getHTMLFormFactory()->buildFormElement('option', '', $value, '', '', $title);

                // Check if it's the selected value
                if ($value == $selectedValue) {
                    $fi->attr('selected', 'selected');
                }

                // Append option to group
                $og->append($fi);
            }

            // Append option group to select
            $this->append($og);
        }

        // Return FormSelect object
        return $this;
    }
public function addOptionsWithGroups($optionGroups = [], $selectedValue = '')
    {
        // Create all options
        foreach ($optionGroups as $groupLabel => $options) {
            // Create option group
            $og = $this->getHTMLFormFactory()->buildFormElement('optgroup', $name = '', $value = '', $id = '', $class = '', $itemValue = '');
            $og->attr('label', $groupLabel);

            // Create all options
            foreach ($options as $value => $title) {
                // Create option
                $fi = $this->getHTMLFormFactory()->buildFormElement('option', '', $value, '', '', $title);

                // Check if it's the selected value
                if ($value == $selectedValue) {
                    $fi->attr('selected', 'selected');
                }

                // Append option to group
                $og->append($fi);
            }

            // Append option group to select
            $this->append($og);
        }

        // Return FormSelect object
        return $this;
    }
public function addOptionsWithGroups ( $ optionGroups = [ ] , $ selectedValue = '' ) { // Create all options foreach ( $ optionGroups as $ groupLabel => $ options ) { // Create option group $ og = $ this -> getHTMLFormFactory ( ) -> buildFormElement ( 'optgroup' , $ name = '' , $ value = '' , $ id = '' , $ class = '' , $ itemValue = '' ) ; $ og -> attr ( 'label' , $ groupLabel ) ; // Create all options foreach ( $ options as $ value => $ title ) { // Create option $ fi = $ this -> getHTMLFormFactory ( ) -> buildFormElement ( 'option' , '' , $ value , '' , '' , $ title ) ; // Check if it's the selected value if ( $ value == $ selectedValue ) { $ fi -> attr ( 'selected' , 'selected' ) ; } // Append option to group $ og -> append ( $ fi ) ; } // Append option group to select $ this -> append ( $ og ) ; } // Return FormSelect object return $ this ; }
Add a list of options grouped in option groups.

@param array  $optionGroups  An array of group labels and a nest array of option value -> title pairs.
@param string $selectedValue The selected value among the option keys.

@return FormSelect

@throws Exception
Add a list of options grouped in option groups .