Joomla 2.5 Custom Field List not SELECTED in display

空扰寡人 提交于 2020-01-24 08:48:07

问题


I have a created a custom field type in my components "/models/fields/time.php" with the following php:

defined('JPATH_BASE') or die;

jimport('joomla.form.formfield');

class JFormFieldTime extends JFormField
{
    protected $type = 'time';

    public function getInput()
    {
        return '<select id="'.$this->id.'" name="'.$this->name.'">'.
        '<option value="08:00:00" > 8:00 AM</option>'.
        '<option value="09:30:00" > 9:30 AM</option>'.
        '</select>';
    }
}

and my course.xml (/models/forms/course.xml) as such:

   <field 
   name="starttime" 
   type="time" 
       label="COM_CEXPRESS_FORM_LBL_COURSE_STARTTIME"
       description="COM_CEXPRESS_FORM_DESC_COURSE_STARTTIME" 
       required="true" 
       filter="safehtml" />

The Form will save the correct value within the database (09:30:00), but the correct value isn't selected="selected" when the form is displayed (8:00 AM). However, if I modify the course.xml field to be:

   <field 
       name="starttime" 
       type="list" 
       label="COM_CEXPRESS_FORM_LBL_COURSE_STARTTIME"
       description="COM_CEXPRESS_FORM_DESC_COURSE_STARTTIME" 
       required="true" 
       filter="safehtml">
          <option value="08:00:00" > 8:00 AM</option>
          <option value="09:30:00" > 9:30 AM</option>
        </field>

the form will correctly display (9:30 AM) the "selected" database value. I used the Joomla Docs per this page:

http://docs.joomla.org/Creating_a_custom_form_field_type


回答1:


You must set the selected option yourself in getInput(). You can get the current value with $this->value.

Instead of printing out the element yourself you could also take use by JHTML:

public function getInput()
{
    $options = array(
        '08:00:00' => '8:00 AM',
        '09:30:00' => '9:30 AM'
    );

    return JHtml::_('select.genericlist', $options, $this->name, null, 'value', 'text', $this->value, $this->id);
}



来源:https://stackoverflow.com/questions/12439922/joomla-2-5-custom-field-list-not-selected-in-display

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!