The Calendar Field and 0000-00-00 date

114 views
Skip to first unread message

Cliff Ford

unread,
Oct 4, 2019, 10:06:13 AM10/4/19
to Joomla! General Development
I have been using the JQuery UI datepicker widget for many years. As I
am rewriting some Joomla 3 code for Joomla 4 I though I would give the
Joomla Calendar widget a try. There is a snag: if the database field is
a date (not datetime) field containing 0000-00-00 then the date
displayed is rubbish (2019-01-11). I have traced this to the Calendar
field getInput method where $this->value !=
Factory::getDbo()->getNullDate() is always true because 0000-00-00 does
not equal 0000-00-00 00:00:00.

So I am wondering weather we should expect the Calendar widget to work
on a Date field. Or have I been doing things wrong all this time. I
might stick with the JQuery datepicker anyway because I like date fields
to be read only to stop manual entry of invalid dates. Or I guess I
could code a custom datepicker based on the Joomla version.

Cliff

Mathew Lenning

unread,
Jan 26, 2020, 8:03:07 PM1/26/20
to Joomla! General Development
From what I can tell the the calendar field expects the corresponding table field to be datetime.
However you can check the value in the JTable instance and format it for date field. 


I loop through the fields in my table and check the fields

                $fields = $this->getFields();
$output = array();

foreach ($src AS $fieldName => $value)
{
if (array_key_exists($fieldName, $fields) && !in_array($fieldName, $ignore))
{
if (is_array($value))
{
$value = json_encode($value);
}

if ($fields[$fieldName]->Type == 'datetime')
{
$date = new \JDate($value);
$value = $date->toSql();
}

                                if ($fields[$fieldName]->Type == 'date')
{
$date = new \JDate($value);
$value = $date->format('Y-m-d);
}

$output[$fieldName] = $value;
}
}

Reply all
Reply to author
Forward
0 new messages