3

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.

1
  • You should always state the version of the framework since the answer will depend on that. Commented Sep 24, 2015 at 19:04

5 Answers 5

5

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

Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

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

Comments

1

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.