I have a set of selects for a date of birth:
<?php echo $this->Form->input('Profile.dob', array('label' => 'Date of Birth'
                                    , 'dateFormat' => 'DMY'
                                    , 'minYear' => date('Y') - 100
                                    , 'maxYear' => date('Y') - 13)); ?>
and want to set the defaults to be the words "DAY MONTH YEAR" in the selects.
I have managed to do this with the gender with:
<?php echo $this->Form->input('Profile.gender', array('label' => 'Gender', 'type' => 'select',
         'options' => array('Male'=>'Male','Female'=>'Female'),'empty'=>'Select Sex')); ?>
but I don't see how to do this with the automagic date input...
Can anyone help? Thanks
Simply add:
'selected'=>date('Y-m-d')
to your array of options.
That example will show the current date. If you need to have a static date, replace it as required. eg:
'selected'=>'2011-12-10'
Obviously for the date and time, use:
'selected'=>date('Y-m-d H:i:s')
or
'selected'=>'2011-12-10 11:13:45'
This way works:
<?php 
echo $this->Form->input(
    'Profile.dob', 
    array(
        'label'         => 'Date of Birth',
        'dateFormat'    => 'DMY',
        'minYear'       => date('Y') - 100,
        'maxYear'       => date('Y') - 13,
        'empty'         => array(
            'day'       => 'DAY',
            'month'     => 'MONTH',
            'year'      => 'YEAR'
            )
        )
    ); 
?>
                        If you don't mind 2 more lines, you can try doing this?
<?php
echo $this->Form->year('Profile.dob', date('Y') - 100, date('Y') - 13, array('empty' => "YEAR"));
echo $this->Form->month('Profile.dob', array('empty' => "MONTH"));
echo $this->Form->day('Profile.dob', array('empty' => 'DAY'));
?>
                        I implemented it like this in cakephp 2.0 above
echo $this->Form->dateTime('dob', 'DMY','', array(
    'value'=>'1987-02-12',
    'empty'=>false,
    'label'=>'Date Of Birth',
    'minYear'=>date('Y')-60,
    'maxYear'=>date('Y')-15)
);
'value' attribute has been added after 2.0 api of cakephp and 'selected' is remove.
Cakephp manual says :The $selected parameter was removed from several methods in FormHelper. All methods now support a $attributes['value'] key now which should be used in place of $selected. This change simplifies the FormHelper methods, reducing the number of arguments, and reduces the duplication that $selected created. The effected methods are:
FormHelper::select()
FormHelper::dateTime()
FormHelper::year()
FormHelper::month()
FormHelper::day()
FormHelper::hour()
FormHelper::minute()
FormHelper::meridian()
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With