开发者

CakePHP Form Helper input date

I have the following code in my view:

$this->Form->input('born');

Which is a date field and I'm look to see if it is possible to have开发者_JS百科 different empty text for each select box like: [Month |v][Day |v][Year |v].

Has anyone come across doing this? Much help is appreciated.


You can do something like this:

echo $this->Form->input('born', array( 'label' => 'Date of birth', 
   'dateFormat' => 'DMY', 
   'minYear' => date('Y') - 70,
   'maxYear' => date('Y') - 18 ));

They will be dropdowns not empty text fields. You can read more about the form helper and automagic forms here:

http://book.cakephp.org/#!/view/1390/Automagic-Form-Elements


I have resorted to using jquery to update the cake empty date values

Cake:

echo $this->Form->input('born', array( 'label' => 'Date of birth', 
   'dateFormat' => 'DMY', 
   'minYear' => date('Y') - 70,
   'maxYear' => date('Y') - 18,
   'empty' => true
 ));

jQuery:

$(function() {
    $('.input.date select[name$="[day]"] option[value=""]').html('-- Day --');
    $('.input.date select[name$="[month]"] option[value=""]').html('-- Month --');
    $('.input.date select[name$="[year]"] option[value=""]').html('-- Year --');
});


echo $this->Form->input('born', array( 'label' => 'Date of birth',
                                       'type'=>'date',
                                       'dateFormat'=> 'DMY',
                                       'minYear' => date('Y') - 70,
                                       'maxYear' => date('Y') - 18 ));


this works form me (CakePHP 2x)

echo $this->Form->input('born', array( 'label' => 'Date of birth', 
   'dateFormat' => 'DMY', 
   'minYear' => date('Y') - 70,
   'maxYear' => date('Y') - 18,
   'empty' => array(
       'day' => '-- Day --', 'month' => '-- Month --', 'year' => '-- Year --',
   )
));


If you can leave the input blank, try this:

echo $this->Form->input('born', array('empty' => true));

If not, check this answer: https://stackoverflow.com/a/11610483/1001673

It's a bit hacky but it'll do wha you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜