4

I wonder what 'date' type needs in system.xml. I keep getting the error :

system.xml (only the field):

<field id="date" translate="label" type="date" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Start sync from :</label>
</field>

error :

Exception #0 (Exception): Output format is not specified. Please specify "format" key in constructor, or set it using setFormat().

If i add a format :

        <field id="date" translate="label" type="date" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="0">
            <label>Start sync from :</label>
            <format>yyyy-MM-dd</format>
        </field>

error :

Element 'format': This element is not expected.

So what wants it? And how can i add time to it? (i need a epoch time eventually in the DB).

EDIT :

This error comes from :

\Magento\Framework\Data\Form\Element\Date : 150-158

$dateFormat = $this->getDateFormat() ?: $this->getFormat();
        $timeFormat = $this->getTimeFormat();
        if (empty($dateFormat)) {
            throw new \Exception(
                'Output format is not specified. ' .
                'Please specify "format" key in constructor, or set it using setFormat().'
            );
        }
13

There is a simple way to achieve that:

app/code/Vendor/Checkout/etc/adminhtml/system.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <tab id="mycustom" translate="label" sortOrder="450">
            <label>My Custom Setting</label>
        </tab>
        <section id="mycustom" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1">
            <class>separator-top</class>
            <label>My Custom Setting</label>
            <tab>mycustom</tab>
            <resource>Magento_Checkout::config</resource>
            <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <field id="mydate" translate="label" type="date" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>My Date Field</label>
                    <frontend_model>Vendor\Checkout\Block\Adminhtml\System\Config\Date</frontend_model>
                </field>
            </group>
        </section>
    </system>
</config>

app/code/Vendor/Checkout/Block/Adminhtml/System/Config/Date.php

<?php

namespace Vendor\Checkout\Block\Adminhtml\System\Config;

class Date extends \Magento\Config\Block\System\Config\Form\Field
{
    public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element)
    {
        $element->setDateFormat(\Magento\Framework\Stdlib\DateTime::DATE_INTERNAL_FORMAT);
        $element->setTimeFormat(null);
        return parent::render($element);
    }
}

Image:

enter image description here

Tested on Magento 2.2.0

Edit by Swat :

If you want to add a time picker :

public function render(AbstractElement $element)
{
    $element->setDateFormat(DateTime::DATE_INTERNAL_FORMAT);
    $element->setTimeFormat('HH:mm:ss');
    $element->setShowsTime(true);
    return parent::render($element);
}

enter image description here

  • 2
    if you can add this to your answer : $element->setDateFormat(DateTime::DATE_INTERNAL_FORMAT); $element->setTimeFormat('HH:mm:ss'); $element->setShowsTime(true); then i will accept it as an answer – CompactCode Nov 21 '17 at 8:58
  • @SwAt.Be you can edit my answer. – Khoa TruongDinh Nov 21 '17 at 8:59
  • @John i didnt test that out but i guess if you do so that it will return nothing. Maybe it has an unset function instead of a setdate function? unsetDateFormat()/ Or a setShowsDate(false) – CompactCode Apr 9 '18 at 15:30
  • @KhoaTruongDinh, how can we use time picker only? without date calendar – fmsthird Apr 10 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.