Magento – How to add time range picker to adminhtml form in Magento 2

adminhtmlmagento2

In Magento 2's devdocs pages there is an example of
time range picker.
enter image description here
But ,i can not find any examples of how to add it in my backend form for creating/editing of custom model.
Have anyone any idea of how to do this?

Also ,i have a question on how to save it in mysql database. Do this picker needs 2 fields ("from" and "to") to store entered time?

Best Answer

Try this,

Under app\code\Namespace\Module\Block\Adminhtml\Custom\Edit\Form.php

$fieldset->addField(
            'from',
            'time',
            [
                'name' => 'from',
                'label' => __('From'),
                'title' => __('From'),
                'required' => true,
                'note' => __('From Timer')
            ]
        );

$fieldset->addField(
            'to',
            'time',
            [
                'name' => 'to',
                'label' => __('To'),
                'title' => __('To'),
                'required' => true,
                'note' => __('To Timer')
            ]
        );

Create the field in database to save the to and from values.

In controller file,

public function saveData()
{
   $data = $this->getRequest()->getPostValue();
   $model = $this->_objectManager->create('Namespace\Module\Model\Custom');
   $model->setData($data);
   $model->save();
}
Related Topic