หน้า 1 จากทั้งหมด 1

Q - How to display date and time value in Joomla 5 xml form filed in custom component ?

โพสต์แล้ว: 05/01/2025 9:59 pm
โดย Raja Pdl
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

โค้ด: เลือกทั้งหมด

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

โค้ด: เลือกทั้งหมด

<?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:
event 1 desc.PNG
event 1 desc.PNG (26.95 KiB) Viewed 2529 times

How can I fix it ?

Re: Q - How to display date and time value in Joomla 5 xml form filed in custom component ?

โพสต์แล้ว: 06/01/2025 2:30 pm
โดย tsukasaz
Try add translateformat="true"

โค้ด: เลือกทั้งหมด

<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"
        translateformat="true"
/>

Re: Q - How to display date and time value in Joomla 5 xml form filed in custom component ?

โพสต์แล้ว: 06/01/2025 11:51 pm
โดย Raja Pdl
tsukasaz เขียน: 06/01/2025 2:30 pm Try add translateformat="true"

โค้ด: เลือกทั้งหมด

<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"
        translateformat="true"
/>
This solution is not working. The result is the same.

Re: Q - How to display date and time value in Joomla 5 xml form filed in custom component ?

โพสต์แล้ว: 20/01/2025 10:38 am
โดย eange08
Raja Pdl เขียน: 06/01/2025 11:51 pm
tsukasaz เขียน: 06/01/2025 2:30 pm Try add translateformat="true"

โค้ด: เลือกทั้งหมด

<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"
        translateformat="true"
/>
This solution is not working. The result is the same.
Please share the code you used to resolve the issue accurately.

Re: Q - How to display date and time value in Joomla 5 xml form filed in custom component ?

โพสต์แล้ว: 20/01/2025 10:43 pm
โดย Raja Pdl
eange08 เขียน: 20/01/2025 10:38 am Please share the code you used to resolve the issue accurately.
retrieving event data from database in model

โค้ด: เลือกทั้งหมด

    public function getItem($id = null)
    {
        $id = (!empty($id)) ? $id : (int) $this->getState('event.id');

        if ($id <= 0) {
            return null;
        }

        $db = Factory::getContainer()->get('DatabaseDriver');
            $query = $db->getQuery(true);
            $query->select('*')->from($db->quoteName('tools_calendar_events'))
            ->where($db->quoteName('id') . ' = ' . (int) $id);
        $db->setQuery($query);

        $item = $db->loadObject();

        if ($item) {
            $item->event_start_date = (new DateTime($item->event_start_date))->format('d-M-Y h:i A');
            $item->event_end_date = (new DateTime($item->event_end_date))->format('d-M-Y h:i A');

        }


if I display event data in alert box, in "edit.php"

โค้ด: เลือกทั้งหมด

$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');
The result is like

โค้ด: เลือกทั้งหมด

Start Date: 07-Jan-2025 11:17 PM
End Date: 07-Jan-2025 11:17 PM
event data result.PNG
event data result.PNG (10.24 KiB) Viewed 2429 times

But it is not displaying in the form input box, so I tried to add the value with JS codes in edit.php, <script> tag.
I also tested with alert box.

โค้ด: เลือกทั้งหมด

        var startDateField = document.querySelector('[name="jform[event_start_date]"]');
        
        startDateField.value =  '<?php echo $startDate; ?>';

        alert('<?php echo $startDate; ?>');
It fills the start date input field with today date, not with actual event date
But in that alert box, it displays date time value correctly.

js - alert box result

alert box result.PNG
alert box result.PNG (10.11 KiB) Viewed 2429 times

But in the form input, the result is today date

today date time value.PNG
today date time value.PNG (11.56 KiB) Viewed 2429 times

In form xml file, 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_type_id"
                type="sql"
                label="EVENT_TYPE"
                hint='SELECT_EVENT_TYPE'
                required="true"
                query="SELECT id AS value, event_type_name AS text FROM tools_calendar_event_types WHERE state = 1 ORDER BY event_type_name ASC"
                key_field="value"
                value_field="text"
            >
                <option value="">SELECT_EVENT_TYPE</option>
            </field>

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

Re: Q - How to display date and time value in Joomla 5 xml form filed in custom component ?

โพสต์แล้ว: 24/01/2025 11:09 am
โดย tsukasaz
Take a look at the sample code from this component.
com_hello.zip
(33.1 KiB) ดาวน์โหลดแล้ว 31 ครั้ง
English Language-1.png
English Language-1.png (58.03 KiB) Viewed 2408 times

Re: Q - How to display date and time value in Joomla 5 xml form filed in custom component ?

โพสต์แล้ว: 30/01/2025 9:51 pm
โดย Raja Pdl
tsukasaz เขียน: 24/01/2025 11:09 am Take a look at the sample code from this component.

com_hello.zip


English Language-1.png
The code related to this bug in other files is the same. For the XML file, I updated it using these sample codes, but the result is still the same. I couldn't fix it.

Re: Q - How to display date and time value in Joomla 5 xml form filed in custom component ?

โพสต์แล้ว: 03/02/2025 11:09 am
โดย eange08
Raja Pdl เขียน: 30/01/2025 9:51 pm
tsukasaz เขียน: 24/01/2025 11:09 am Take a look at the sample code from this component.

com_hello.zip


English Language-1.png
The code related to this bug in other files is the same. For the XML file, I updated it using these sample codes, but the result is still the same. I couldn't fix it.
Please share the code you used to resolve the issue accurately.

Re: Q - How to display date and time value in Joomla 5 xml form filed in custom component ?

โพสต์แล้ว: 05/02/2025 10:13 pm
โดย Raja Pdl
eange08 เขียน: 03/02/2025 11:09 am Please share the code you used to resolve the issue accurately.
retrieving event data from database in model

โค้ด: เลือกทั้งหมด

    public function getItem($id = null)
    {
        $id = (!empty($id)) ? $id : (int) $this->getState('event.id');

        if ($id <= 0) {
            return null;
        }

        $db = Factory::getContainer()->get('DatabaseDriver');
            $query = $db->getQuery(true);
            $query->select('*')->from($db->quoteName('tools_calendar_events'))
            ->where($db->quoteName('id') . ' = ' . (int) $id);
        $db->setQuery($query);

        $item = $db->loadObject();

        if ($item) {
            $item->event_start_date = (new DateTime($item->event_start_date))->format('d-M-Y h:i A');
            $item->event_end_date = (new DateTime($item->event_end_date))->format('d-M-Y h:i A');

        }


if I display event data in alert box, in "edit.php"

โค้ด: เลือกทั้งหมด

$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');
The result is like

โค้ด: เลือกทั้งหมด

Start Date: 07-Jan-2025 11:17 PM
End Date: 07-Jan-2025 11:17 PM
event data result.PNG
event data result.PNG (10.24 KiB) Viewed 2258 times

But it is not displaying in the form input box, so I tried to add the value with JS codes in edit.php, <script> tag.
I also tested with alert box.

โค้ด: เลือกทั้งหมด

        var startDateField = document.querySelector('[name="jform[event_start_date]"]');
        
        startDateField.value =  '<?php echo $startDate; ?>';

        alert('<?php echo $startDate; ?>');
It fills the start date input field with today date, not with actual event date
But in that alert box, it displays date time value correctly.

js - alert box result

alert box result.PNG
alert box result.PNG (10.11 KiB) Viewed 2258 times

But in the form input, the result is today date

today date time value.PNG
today date time value.PNG (11.56 KiB) Viewed 2258 times

In form xml file, 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_type_id"
                type="sql"
                label="EVENT_TYPE"
                hint='SELECT_EVENT_TYPE'
                required="true"
                query="SELECT id AS value, event_type_name AS text FROM tools_calendar_event_types WHERE state = 1 ORDER BY event_type_name ASC"
                key_field="value"
                value_field="text"
            >
                <option value="">SELECT_EVENT_TYPE</option>
            </field>

            <field
                name="event_start_date"
                type="calendar"
                label="START_DATE"
                hint="ENTER_EVENT_START_DATE"
                required="true"
                showtime="true"
                timeformat="12"
                filter="server_utc"
                format="%d-%b-%Y %H:%M"
            />
            <field
                name="event_end_date"
                type="calendar"
                hint="ENTER_EVENT_END_DATE"
                label="END_DATE"
                required="true"
                showtime="true"
                timeformat="12"
                filter="server_utc"
                format="%d-%b-%Y %H:%M"
            />
        </fieldset>
</form>