0

I am developing a custom module where there are many fields text,select,multiselect..etc in admin form.

Today I have added one field in admin of type time ,below is the code to add new field

$timeFormat = $this->_localeDate->getTimeFormat(\IntlDateFormatter::MEDIUM);

$actionTime = $fieldset->addField(
                    'action_time',
                    'time',
                    [   
                        'name'      => 'action_time',
                        'label'     => __('Time'),
                        'class'     => 'required-entry',
                        'required'  => true,
                        'time_format'=> $timeFormat,
                        'time'=>true
                    ]
                );

and in installSchema.php I have put below code to add column in table

)->addColumn(
             'action_time',
            \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
            255,
            ['nullable' => false],
            'Action Time'
        )

Field created successfully but it throwing error when saving form. enter image description here

Fatal error: Uncaught TypeError: DateTime::__construct() expects parameter 1 to be string, array given in /var/www/html/Magento2/vendor/magento/framework/Stdlib/DateTime.php:57 Stack trace: #0 /var/www/html/Magento2/vendor/magento/framework/Stdlib/DateTime.php(57): DateTime->__construct(Array) #1 /var/www/html/Magento2/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php(2764): Magento\Framework\Stdlib\DateTime->formatDate(Array, true) #2 /var/www/html/Magento2/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php(3001): Magento\Framework\DB\Adapter\Pdo\Mysql->formatDate(Array) #3 /var/www/html/Magento2/vendor/magento/framework/Model/ResourceModel/AbstractResource.php(183): Magento\Framework\DB\Adapter\Pdo\Mysql->prepareColumnValue(Array, Array) #4 /var/www/html/Magento2/vendor/magento/framework/Model/ResourceModel/Db/AbstractDb.php(541): Magento\Framework\Model\ResourceModel\AbstractResource->_prepareDataForTable(Object(Vendorname\Modulename\Model\Grid), 'manage_feed_dat...') #5 /var/www/html/Magento2/vendor/magento/framework in /var/www/html/Magento2/vendor/magento/framework/Stdlib/DateTime.php on line 57

I have debugged but not found any issue.Please help me.

  • it could be an issue with the time format, test it with static format like 'time_format' => 'hh:mm:ss' or 'time_format' => 'H:i:s', – fmsthird Apr 25 at 10:09
  • 1
    @magefms, I have also tried this but not working,same error. – akgola Apr 25 at 10:10
  • You need to add "default" option with value \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT – Anju Apr 25 at 10:45
  • ok @Anju,I am trying now. – akgola Apr 25 at 10:47
  • Hi @Anju, this is also not working. – akgola Apr 25 at 11:02
0

first, sorry if my english is bad, i hope u know my ideal. type time in formfield return array, like Array [ [0] => 04 , [1] => 20, [2] => 54 ] : 04h:20m:54s.
To save value time field, try it $data['action_time'] = $data['action_time'][0] . ':' . $data['action_time'][1] . ':' .$data['action_time'][2] ;

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.