Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP Default values for date select

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

like image 973
Cameron Avatar asked Dec 31 '11 12:12

Cameron


4 Answers

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'

like image 148
Ross Avatar answered Sep 20 '22 14:09

Ross


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'
            )
        )
    ); 
?>
like image 42
Nicolás Apolonia Avatar answered Sep 17 '22 14:09

Nicolás Apolonia


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

?>
like image 21
Mo3z Avatar answered Sep 17 '22 14:09

Mo3z


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()
like image 31
Dashrath Avatar answered Sep 18 '22 14:09

Dashrath