Marco Maranao
Marco Maranao

Reputation: 31

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

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

Upvotes: 3

Views: 18167

Answers (5)

thanat
thanat

Reputation: 334

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

Upvotes: 1

Gabe
Gabe

Reputation: 330

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.

Upvotes: 0

Vipin
Vipin

Reputation: 21

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

Upvotes: 2

peltman
peltman

Reputation: 31

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

Upvotes: 3

Chuck Burgess
Chuck Burgess

Reputation: 11574

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

Upvotes: 5

Related Questions