简体   繁体   中英

CakePHP 3.2 changing default order of date form input

I am trying to get cakephp to generate the following HTML <div class="input date"><label>Leverdatum </label><select name="Leverdatum [day]">...</select><select name="Leverdatum [month]">...</select><select name="Leverdatum [year]">...</select></div> so that the order of inputting the date is day-month-year. However cake is generating it in the order of year-day-month. The code I'm using on my .ctp page is echo $this->Form->input('Leverdatum ', ['type' => 'date', 'default' => date("dmy", strtotime($orders->DELIVERYDATE))]); but this only fills in the correct date and doesn't order the select boxes the way I want them to. I have found that in previous versions of cakePHP you could use 'dateFormat' => 'DMY' to achieve this. But this has apperantly been removed from cakePHP.

Now I did find a 'fix' which is to edit the cakePHP files at \\Cake\\View\\Helper\\FormHelper in protected $_defaultConfig change the variable 'dateWidget' => '{{year}}{{month}}{{day}}{{hour}}{{minute}}{{second}}{{meridian}}' to the order I want it in 'dateWidget' => '{{day}}{{month}}{{year}}{{hour}}{{minute}}{{second}}{{meridian}}' . But I don't really want to have to do this every time I want to update my cakePHP version.

My question is: Is there a way to tell cake what order I want my date to be in, aside from editting their source files?

It's all explained in the manual

Anyway instead of editing the $_defaultConfig create a file in your config folder an name it something like my_form_templates.php

and in it write your templates

<?php
    return [
        'dateWidget' => '{{day}}{{month}}{{year}}{{hour}}{{minute}}{{second}}{{meridian}}'
    ];
?>

then when you load FormHelper you can do

 $this->loadHelper('Form', [
        'templates' => 'my_form_templates',
    ]);

this tells cake to use your templates instead of the default ones

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM