9

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?

0

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();
}
  • I've tried this earlier. This code adds two time fields positioned vertically. If I need 7 time range pickers (for each day of week) it looks very cumbersome. I wanted to place it at least in one line but much desirable to have it exact as on picture above. My opinion is that this time range picker exists only in Magento 2 design patterns and does not have implementation yet. For now I made it using single text field and Bootstrap Date Range Picker, customized it to show only time range. Result I save in DB in text format ("10:00-15:00" for example) which suits for my tasks. – jurgen Jun 8 '16 at 6:33

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.