CakePHP Form Helper input date

18,094

Solution 1

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

Solution 2

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 --');
});

Solution 3

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

Solution 4

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 --',
   )
));
Share:
18,094
Marco Maranao
Author by

Marco Maranao

Updated on June 04, 2022

Comments

  • Marco Maranao
    Marco Maranao almost 2 years

    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 different empty text for each select box like: [Month |v][Day |v][Year |v].

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