问题
I am trying to create a custom field form for template parameter for Joomla 3, by following instruction from this page Creating a custom form field type
Here are my codes :
class JFormFieldMy extends JFormField {
protected $type = 'my';
public function getInput() {
return '<select id="'.$this->id.'" name="'.$this->name.'">'.
'<optgroup label="First">'.
'<option value="1">One</option>'.
'<option value="2">Two</option>'.
'<option value="3">Three</option>'.
'</optgroup>'.
'<optgroup label="Second">'.
'<option value="4">Four</option>'.
'<option value="5">Five</option>'.
'<option value="6">Six</option>'.
'</optgroup>'.
'</select>';
}
}
It works good, the value is saved, but the selected value doesn't have the selected="selected" state so the dropdown list will always show the option 'One' when I choose / the actual value is 'Two'
I have read this solution : Joomla 2.5 Custom Field List not SELECTED in display but that's for generic list type not for grouped list I wanted.
Anyone can help me? Thanks
回答1:
You are not setting the selected element of the list:
<option value="the_value" selected>....</option>
Another approach: instead of deriving your class from JFormField you should derive it from the abstract class JHtmlList (you will find it on libraries/cms/html/list.php
)
You may start taking libraries/cms/form/field/limitbox.php
as an example.
来源:https://stackoverflow.com/questions/22660483/joomla-3-2-grouped-list-custom-field-list-doesnt-have-selected-value