CakePHP: date field with default values

I am using CakePHP 2.1 and have a date field to get the user's birthday:

eg.

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 tried to set the default values ​​using an array, however it just makes them have a DATE value by default. How to get it so that each drop-down menu has the correct meaning?

+2
source share
6 answers

It's a bit hacky and pretty ugly, but since a parameter emptydoesn't support multiple values, this is probably the easiest solution - if you don't want to rewrite the whole function dateTime(). str_replace, unfortunately, does not allow to limit the number of replacements, so we must 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;
+6

. , : , :

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'));
+4
<?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

));
?>

EDIT:-( jquery) jQuery script jquery.js.

<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>

enter image description here

#dateDay, #dateMonth, #dateYear option.... , cakephp.

+3
<?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'));
?>
+3

echo $this->Form->dateTime('dob', 'DMY','', array('value'=>'1987-02-12','empty'=>false,'label'=>'','minYear'=>date('Y')-60,'maxYear'=>date('Y')-15));

'value' 2.0 api cakephp 'selected' is remove

2.0 $

$selected FormHelper. $attributes ['value'], $selected. FormHelper, , . :

FormHelper::select()
FormHelper::dateTime()
FormHelper::year()
FormHelper::month()
FormHelper::day()
FormHelper::hour()
FormHelper::minute()
FormHelper::meridian()
0

All Articles