2012-09-15 53 views
4

沒有選擇在我的組件「/models/fields/time.php」我有一個創建一個自定義字段類型用下面的PHP:的Joomla 2.5自定義字段列表中顯示

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>'; 
    } 
} 

和我course.xml (/models/forms/course.xml)爲這樣:

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

表單將保存數據庫(9點30分00秒)內的正確的值,但是未選擇正確的值=「選擇」當表單顯示時(8:00 AM)。但是,如果我將course.xml字段修改爲:

<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> 

表單將正確顯示(上午9:30)「選定」數據庫值。我用的Joomla文件每本頁面:

http://docs.joomla.org/Creating_a_custom_form_field_type

+0

問題仍然得不到解決?如果是這樣,請提供更多有關您問題的內容。 –

回答

4

您必須getInput()設置自己所選擇的選項。您可以通過$ this->值獲取當前值。

而不是打印出自己的元素,你也可以採取使用的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); 
} 

+0

謝謝,Fnatte!很棒。 – ghires

+0

@Fnatte任何想法如何實現你的解決方案分組列表類型?我在這裏開始了一個新的問題http://stackoverflow.com/questions/22660483/joomla-3-2-grouped-list-custom-field-list-doesnt-have-selected-value謝謝! – webchun

相關問題