Q - How to display date and time value in Joomla 5 xml form filed in custom component ?
โพสต์แล้ว: 05/01/2025 9:59 pm
In my custom Joomla 5 component, I need to fetch data from the database and display it on the edit page. All the data, such as the event title and event description, displays correctly in the form. However, the event start date and end date are not showing up. How can I fix this?
in models/forms/event.xml file
I tried timeformat="24", but it is not working.
I also tried removing format="....", but it is still not working.
In "views/event/edit.php," I tried outputting the event start date and end date values using an alert box, and the values were displayed correctly in the alert. However, the event start date and end date values are not appearing in the form input fields.
in "views/event/edit.php"
Screenshot:
How can I fix it ?
in models/forms/event.xml file
โค้ด: เลือกทั้งหมด
<?xml version="1.0" encoding="utf-8"?>
<form>
<fieldset name="event">
<field
name="id"
type="hidden"
label="JGLOBAL_FIELD_ID"
description="JGLOBAL_FIELD_ID_DESC"
/>
<field
name="event_title"
type="text"
hint="ENTER_EVENT_TITLE"
label="EVENT_TITLE"
required="true"
maxlength="100"
/>
<field
name="event_description"
type="text"
label="EVENT_DESCRIPTION"
hint="ENTER_EVENT_DESCRIPTION"
required="true"
maxlength="255"
/>
<field
name="event_start_date"
type="calendar"
label="START_DATE"
hint="ENTER_EVENT_START_DATE"
required="true"
showtime="true"
timeformat="12"
format="%d-%b-%Y %I:%M %p"
/>
<field
name="event_end_date"
type="calendar"
hint="ENTER_EVENT_END_DATE"
label="END_DATE"
required="true"
showtime="true"
timeformat="12"
format="%d-%b-%Y %I:%M %p"
/>
</fieldset>
</form>
I also tried removing format="....", but it is still not working.
In "views/event/edit.php," I tried outputting the event start date and end date values using an alert box, and the values were displayed correctly in the alert. However, the event start date and end date values are not appearing in the form input fields.
in "views/event/edit.php"
โค้ด: เลือกทั้งหมด
<?php
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;
HTMLHelper::_('behavior.keepalive');
HTMLHelper::_('behavior.formvalidator',false);
$formData = $this->form->getData();
$startDate = $formData->get('event_start_date');
$endDate = $formData->get('event_end_date');
Factory::getApplication()->enqueueMessage('Start Date: ' . $startDate, 'message');
Factory::getApplication()->enqueueMessage('End Date: ' . $endDate, 'message');
?>
<form action="<?php echo Route::_('index.php?option=com_tools_calendar_events&controller=event'); ?>" method="post" name="adminForm" id="eventtype-form" class="form-validate">
<div class="form-horizontal">
<fieldset>
<?php foreach ($this->form->getFieldset('event') as $field) : ?>
<div class="control-group">
<div class="control-label">
<?php echo $field->label; ?>
</div>
<div class="controls">
<?php echo $field->input; ?>
</div>
</div>
<?php endforeach; ?>
</fieldset>
</div>
<input type="hidden" name="task" value=""/>
<?php echo HTMLHelper::_('form.token'); ?>
</form>
Screenshot:
How can I fix it ?