CakePHP 3: change order in dateWidget

情到浓时终转凉″ 提交于 2019-12-11 04:00:40

问题


I found on the CakePHP developer's guide the following hint on how to adjust the order of the fields when using the date form input. It says

To control the order of inputs, and any elements/content between the inputs you can override the dateWidget template.

However, I cannot find anywhere a hint on how to change the order and use this in a view. Can you give hints?


回答1:


You can configure the DateWidget template when initializing the Form helper

// in MyController.initialize()
$this->loadHelper('Form', [
    'dateWidget' => '{{year}}{{month}}{{day}}{{hour}}{{minute}}{{second}}{{meridian}}'
]);

or you can override the template using the templates function in the form component

$this->Form->templates([
    'dateWidget' => '{{year}}{{month}}{{day}}{{hour}}{{minute}}{{second}}{{meridian}}'
]);

@see http://book.cakephp.org/3.0/en/views/helpers/form.html#customizing-the-templates-formhelper-uses




回答2:


Just for reference... this seems to work as well placed directly in the view:

<?= $this->Form->input('date_of_birth',               
    [ 
    'templates' => [ 
        'dateWidget' => '<div class="clearfix">{{month}}<span class="spacer-date">/</span>{{day}}<span class="spacer-date">/</span>{{year}}</div>',
    ],
    'type' => 'date',
    'label' => 'Date of Birth',
    'empty' => array(
        'month' => '(month)',
        'day'   => '(day)',
        'year'  => '(year)'
    ),
    'minYear' => date('Y') - 70,
    'maxYear' => date('Y') - 18
    ]
) ?>


来源:https://stackoverflow.com/questions/30876688/cakephp-3-change-order-in-datewidget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!