CakePHP Default values for date select
Asked Answered
U

7

5

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

Unwearied answered 31/12, 2011 at 12:57 Comment(1)
You could potentially add a few lines of JS to append to the beginning of the <selects> the text? Short of modifying the form helper - I can't think of a less "hackish" way.Ghats
H
2

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

?>
Happening answered 31/12, 2011 at 19:18 Comment(4)
Gives me this error: Fatal error: Unsupported operand types in /Users/cameron/Sites/social/lib/Cake/View/Helper/FormHelper.php on line 1816Unwearied
@Cameron, may be I am missing some braces or you wanna try $this->Form->year(....).Happening
@Cameron, I apologize I should have mentioned. It would work only for cake 1.3+Happening
This is the correct answer, but on the year() call you should do this: echo $this->Form->year('Profile.dob', array('minYear' => date('Y') - 100, 'maxYear' => date('Y') - 13, 'empty' => "YEAR"));Duckbill
T
12

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'

Tuscany answered 31/12, 2011 at 16:20 Comment(2)
What about DAY MONTH YEAR as in the actual words!Unwearied
'selected' is the keyword I was looking for, thanks for sharing :)Leela
M
8

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'
            )
        )
    ); 
?>
Muddy answered 17/9, 2013 at 0:30 Comment(1)
Nice! This exactly answers the original question (which I had as well) and does it without switching to three separate statements.Seagoing
H
2

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

?>
Happening answered 31/12, 2011 at 19:18 Comment(4)
Gives me this error: Fatal error: Unsupported operand types in /Users/cameron/Sites/social/lib/Cake/View/Helper/FormHelper.php on line 1816Unwearied
@Cameron, may be I am missing some braces or you wanna try $this->Form->year(....).Happening
@Cameron, I apologize I should have mentioned. It would work only for cake 1.3+Happening
This is the correct answer, but on the year() call you should do this: echo $this->Form->year('Profile.dob', array('minYear' => date('Y') - 100, 'maxYear' => date('Y') - 13, 'empty' => "YEAR"));Duckbill
K
2

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()
Kaitlin answered 2/4, 2013 at 7:7 Comment(0)
B
2

Also, are you really sure about what you really mean?

You seem to be confusing default value and empty value. If you set default value as DAY MONTH YEAR using 'selected', your code won't work because DAY MONTH YEAR isn't a valid date. Using

'empty' => array(
  'day' => 'DAY',
  'month' =>'MONTH',
  'year' =>'YEAR'
);

looks like what you're looking for, inviting the user to enter a date.

Beeler answered 19/8, 2015 at 18:12 Comment(0)
E
0

Cakephp set selected to the empty value if the corresponding date request data is null, therefore simply set it to null prior to echoing the date input fields:

$this->request->data['Profile']['dob'] = null;

echo $this->Form->month('Profile.dob', array('empty' => "MONTH"));
// do the same for year and day

the empty value (in your case "DATE", "MONTH", "YEAR" fields) will be pre-selected in the input form

Ectogenous answered 25/7, 2013 at 2:51 Comment(0)
B
0

This is what currently works for me in cakephp 2.5:

echo $this->Form->input('fecha_pos_fijacion', array(
    'label' => 'Fecha de fijación',
    'dateFormat' => 'DMY',
    'minYear' => date('Y'),
    'maxYear' => date('Y')+5,
    'orderYear' => 'asc',
    'selected' => date('Y-m-1')
    )

);

This is a bit more elaborated, it'd give you a default value set to the 1st day of current month. Possible values for year are between current and 5 years ahead, displayed in ascending order.

Even more complete:

echo $this->Form->input('fecha_transporte', array(
                    'label' => '',
                    'dateFormat' => 'DMY',
                    'minYear' => date('Y'),
                    'maxYear' => date('Y')+5,
                    'orderYear' => 'asc',
                    'selected' => date('Y-m-1', strtotime("+30 days"))
                    )
            );

Here default is 1st day of next month

Beeler answered 19/8, 2015 at 18:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.