简体   繁体   中英

CakePHP 2.x Troubles Using minYear/maxYear Params

I use CakePHP V. 2.3.4 on Windows 7 32 Bits, I'm trying to use maxYear and minYear parameters, but I don't get the correct values, the code that I use is next:

echo $this->Form->input(
    'date_birth',
    array(
        'dateFormat'=>'DMY',
        'minYear'=>date('Y')-100,
        'maxYear'=>date('Y')-18
)

);

The values shown by cake are: Min Year: 1913, Max Year: 2013 .
The correct values should be: Min Year: 1913, Max Year: 1995 .

Also I tried to put:

echo $this->Form->input(
    'date_birth',
    array(
        'dateFormat'=>'DMY',
        'maxYear'=>date('Y')-18
    )
);

But the result is incorrect: from 1993 to 2013 .

Please help me.

I found that adding a default value to the input corrects this issue; essentially, CakePHP will default the date entry to the current date, and it looks like if you try to set maxYear to something earlier than your default, it ignores maxYear in favor of your default.

So:

echo $this->Form->input(
    'dob',
    array(
        'type' => 'date',
        'selected' => array(
            'year'=>date('Y')-18
        ),
        'minYear' => date('Y') - 100,
        'maxYear' => date('Y') - 18
    )
);

As per cakephp document

you can try below code

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

i think it will sure work for you.

I just used the current master branch (2.3.5) and uses your exact snippet

$result = $this->Form->input('date_of_birth', array(
    'dateFormat' => 'DMY',
    'minYear' => date('Y') - 100,
    'maxYear' => date('Y') - 18));

and got:

<div class="input text">
    <label for="date_of_birth">Date Of Birth</label>
    <input name="data[date_of_birth]" dateFormat="DMY" minYear="1913" maxYear="1995" type="text" id="date_of_birth"/>
</div>

I dont know what you are doing. But it sure looks like it is not cake's fault here..

Note that since 2.3.4 the value range is auto-expected as noted above in the comment if you pass in a year outside of this range as preselect/default value.

I finally got it to work as follows:

<?php echo $this->Form->input('date_birth', array('type'=>'date',
    'label' => 'Date of birth',
    'dateFormat' => 'DMY',
    'empty' => true,
    'minYear' => date('Y')-100,
    'maxYear' =>date('Y')-18,
    )
    );
?>

Note that the solution was to add: 'empty' => true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM