CakePHP:具有默认值的日期字段 [英] CakePHP: Date field with default values
问题描述
我使用CakePHP 2.1并有一个日期字段来获取用户的出生日期:
I'm using CakePHP 2.1 and have a date field to get a users date of birth:
例如
echo $this->Form->input('Profile.dob', array('label' => 'Date of Birth'
, 'dateFormat' => 'DMY'
, 'empty' => array('DATE','MONTH','YEAR')
, 'minYear' => date('Y') - 110
, 'maxYear' => date('Y') - 0));
正如你所看到的,我试图使用数组设置默认值,所有都具有默认值DATE。如何获得它,使每个下拉列表都有正确的值?
As you can see I have tried to set the default values using an array, however it just makes them all have a default value of DATE. How do I get it so that each of the dropdowns has the correct value?
推荐答案
这是一个hack,很丑,但由于 It's a bit of a hack and quite ugly, but since the 这篇关于CakePHP:具有默认值的日期字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! empty
选项似乎不支持多个值,它可能是最简单的解决方案 - 除非你要重写整个 dateTime() / code>函数。
str_replace
不幸的是不允许限制替换的数量,这就是为什么我们必须诉诸 preg_replace
。
empty
option doesn't appear to support multiple values it's probably the easiest solution - unless you want to rewrite the whole dateTime()
function. str_replace
unfortunately doesn't allow limiting the number of replacements, which is why we have to resort to preg_replace
.$placeholder = '[RandomStringWhichDoesNotAppearInTheMarkup]';
$out = $this->Form->input('Profile.dob', array('label' => 'Date of Birth'
, 'dateFormat' => 'DMY'
, 'empty' => $placeholder
, 'minYear' => date('Y') - 110
, 'maxYear' => date('Y') - 0));
$escapedPlaceholder = preg_quote($placeholder, '/');
$out = preg_replace("/$escapedPlaceholder/", 'DATE', $out, 1);
$out = preg_replace("/$escapedPlaceholder/", 'MONTH', $out, 1);
$out = preg_replace("/$escapedPlaceholder/", 'YEAR', $out, 1);
echo $out;