问题
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