Reputation: 28783
I'm using CakePHP 2.1 and have a date field to get a users date of birth:
e.g.
echo $this->Form->input('Profile.dob', array('label' => 'Date of Birth'
, 'dateFormat' => 'DMY'
, 'empty' => array('DATE','MONTH','YEAR')
, 'minYear' => date('Y') - 110
, 'maxYear' => date('Y') - 0));
As you can see I have tried to set the default values using an array, however it just makes them all have a default value of DATE. How do I get it so that each of the dropdowns has the correct value?
Upvotes: 2
Views: 10790
Reputation: 2189
I implemented it like this
echo $this->Form->dateTime('dob', 'DMY','', array('value'=>'1987-02-12','empty'=>false,'label'=>'','minYear'=>date('Y')-60,'maxYear'=>date('Y')-15));
'value' attribute has been added after 2.0 api of cakephp and 'selected' is remove
2.0 updates $selected parameter removed
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()
Upvotes: 0
Reputation: 7762
<?php echo $this->Form->input('birth_dt', array( 'label' => 'Date of birth'
, 'dateFormat' => 'DMY'
, 'minYear' => date('Y') - 70
, 'maxYear' => date('Y') - 18 ));
?>
//OR
<?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'));
?>
Upvotes: 3
Reputation: 8100
The feature you want is now implemented and will be available in next release. Currently you can checkout the master branch from github to get this patch. Check the test cases in linked commit for usage example. If you are currently using cakephp 2.1, read the 2.1 to 2.2 migration guide on the manual as the master branch is the 2.2 dev branch.
Upvotes: 5
Reputation: 1540
I have checked this It will give same output which is required : This code will generate same dropdown box with given default value and the post data will be same :
echo $this->Form->day('Profile.dob', array('empty'=>'Day'));
echo $this->Form->month('Profile.dob', array('empty'=>'Month'));
echo $this->Form->year('Profile.dob', 1950, date('Y'),array('empty'=>'Year'));
Upvotes: 4
Reputation: 473
<?php
echo $form->input('date', array(
'type' => 'date',
'label' => 'Date',
'empty' => TRUE,
'minYear' => 2000,
'dateFormat' => 'DMY',
'maxYear' => date('Y'),
'minYear' => date('Y') - 10
# default order m/d/y
));
?>
Its woking like charm
EDIT:-(You can do this with simple jquery code) In case you want customised date empty value ADD this jquery script in you header with jquery.js included.
<script type="text/javascript">
// var valid=true;
jQuery(document).ready( function() {
$("#dateDay option:first").text('DAY');
$("#dateMonth option:first").text('MONTH');
$("#dateYear option:first").text('YEAR');
});
</script>
change id #dateDay, #dateMonth, #dateYear with your option field id....You can customise this making it work for every cakephp-date field.
Upvotes: 3
Reputation: 2306
It's a bit of a hack and quite ugly, but since the empty
option doesn't appear to support multiple values it's probably the easiest solution - unless you want to rewrite the whole dateTime()
function. str_replace
unfortunately doesn't allow limiting the number of replacements, which is why we have to resort to preg_replace
.
$placeholder = '[RandomStringWhichDoesNotAppearInTheMarkup]';
$out = $this->Form->input('Profile.dob', array('label' => 'Date of Birth'
, 'dateFormat' => 'DMY'
, 'empty' => $placeholder
, 'minYear' => date('Y') - 110
, 'maxYear' => date('Y') - 0));
$escapedPlaceholder = preg_quote($placeholder, '/');
$out = preg_replace("/$escapedPlaceholder/", 'DATE', $out, 1);
$out = preg_replace("/$escapedPlaceholder/", 'MONTH', $out, 1);
$out = preg_replace("/$escapedPlaceholder/", 'YEAR', $out, 1);
echo $out;
Upvotes: 6