问题
I have two fields in my admin/componsents/com_xxxxx/models/forms/xxxxx.xml file.
these feed into an input form for administrators on the back end of Joomla 2.5
<field name="f_start" type="calendar" class="inputbox"
required="true"
format="%Y-%m-%d %H:%M:%S"
default="0000-00-00 09:30:00"
label="COM_xxxxx_F_START"
description="COM_xxxxx_F_START_DESC"
filter="safehtml" />
<field name="f_end" type="calendar" class="inputbox"
required="true"
format="%Y-%m-%d %H:%M:%S"
default="0000-00-00 19:30:30"
label="COM_xxxxx_F_END"
description="COM_xxxxx_F_END_DESC"
filter="safehtml" />
These are essentially start and end dates of when an article is published. however when selecting the datepicker/calendar icon and choosing a date the field is updated to the date chosen but keeps the 09:30:00 default start time. this seems to work for times between 01:30:00 through to 11:30 any afternoon times get reset to now when a date is selected.
can anyone explain why? or how to keep the default times on the date selector?
if the end date could also default to 28 days from the start date?
thanks in advance.
回答1:
When clicking on the calendar icon, the calendar widget tries to position itself on the date contained in the corresponding text field. As 0000-00-00
is an invalid date, the Date.parseDate
function in media/system/js/calendar-uncompressed.js
tries to guess the date from all components of the format string. 0000-00-00 09:30:00
is recognized as Sep 30, because 09 < 12
, so it looks like a month number, and thus it returns Sep 30, 9:30
. On the other hand, 0000-00-00 19:30:00
is not recognized as any valid date, and the function returns today
. Hence the difference in the time part.
If you look at the XML form files for com_content for example, you'll see that they don't use default values for the calendar fields.
You could however create a custom field type derived from JFormFieldCalendar which would give you full flexibility. For example:
forms/whatever.xml
<field name="f_start" type="PubDateCalendar"
format="%Y-%m-%d %H:%M:%S" default="start" ... />
<field name="f_end" type="PubDateCalendar"
format="%Y-%m-%d %H:%M:%S" default="end" ... />
fields/pubdatecalendar.php
<?php
defined('_JEXEC') or die;
jimport('joomla.form.helper');
JFormHelper::loadFieldClass('calendar');
class JFormFieldPubDateCalendar extends JFormFieldCalendar
{
public $type = 'PubDateCalendar';
protected function getInput()
{
$format = $this->element['format']
? (string) $this->element['format']
: '%Y-%m-%d';
if ($this->element['default'] == 'start') {
$this->value = strftime($format);
} else if ($this->element['default'] == 'end') {
$this->value = strftime($format, time() + 28 * 24 * 60 * 60);
}
return parent::getInput();
}
}
?>
or whatever suits your particular application better.
回答2:
Maybe this will be helpful for someone like me who was looking for answer on this question but on current version of Joomla 3.3.6:
<field
name="birth_day"
type="calendar"
label="COM_PERSONS_PERSON_FIELD_BIRTHDAY_LABEL"
description="COM_PERSONS_PERSON_FIELD_BIRTHDAY_DESC"
class="inputbox"
size="20"
format="%Y-%m-%d"
filter="user_utc" />
来源:https://stackoverflow.com/questions/11648167/joomla-2-5-calendar-field-type-in-custom-form-date-and-time-selection