How to render optional attributes with JHtmlSelect::option()

43 views
Skip to first unread message

Tino

unread,
Jul 4, 2017, 10:08:06 AM7/4/17
to Joomla! General Development
I am stuck with class JHtmlSelect's function option() where it seems to be possible to pass additional arguments via optKey and optText to override the default arguments value and text. I am trying to add a datalist to a text input field with the data list entries having more than just the value-attribute like so:

<input type="text" id="jform_myfield" list="jform_myfield_datalist.../>
<datalist id="jform_myfield_datalist">
  <option value="SLG1" data-value="1" data-...="...">SLG1</option>
  <option value="SLG2" data-value="2" data-...="...">SLG2</option>
  <option value="SLG3" data-value="3" data-...="...">SLG3</option>
</datalist>

Following the function documentation (at least as how I can understand it conceptionally) I pass the following data to JHtmlSelect::option():

$arrAttribs = array(
  'option.attr' => 'attr'
  ,'attr' => array('data-value' => 'TESTVAL', 'data-rule-required' => 'true', 'data-msg-required' => 'This option is required')
  // ,'option.disable' => 'TEST.OPTDISABLE'
  // ,'option.key'  => 'data-value'
  // ,'option.label' => 'TEST.OPTLABEL'
  // ,'option.text' => 'TEST.OPTTEXT'
);

$objOption  = JHtml::_('select.option', 
(string) $option['value'],
  JText::alt(trim((string) $option), preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname)),
  $arrAttribs
);

Unfortunately I can't seem to get the desired output. As soon as I uncomment any other optional argument JHtmlSelect::option() reports this error:

Undefined property: stdClass::$value in \layouts\joomla\form\field\text.php on line 86

Anybody who may explain the correct way to pass optional arguments that make the target method render list options with custom attributes as described? And mabe you can explain in more detail the concept of this override as the function doc alone don't suffice and there is no example use case within the Joomla code base.

Viper

unread,
Jul 4, 2017, 1:07:12 PM7/4/17
to Joomla! General Development
It's not possible to generate custom options using JHtmlSelect.

Tino

unread,
Jul 4, 2017, 1:50:32 PM7/4/17
to Joomla! General Development
The question is by no means related to the generation of custom options. Thanks.
Reply all
Reply to author
Forward
0 new messages