Joomla 2.5 Custom field list not selected on display

I have 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 .xml course (/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 in the database (09:30:00), but the correct value will not be selected = "selected" when the form is displayed (8:00 AM). However, if I change the course.xml field as follows:

  <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 a.m.) the value of the "selected" database. I used Joomla Docs for this page:

http://docs.joomla.org/Creating_a_custom_form_field_type

+4
source share
1 answer

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

Instead of printing the element yourself, you can also use 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); } 

+4
source

Source: https://habr.com/ru/post/1434388/


All Articles