Noe7sv
Noe7sv

Reputation: 11

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.

Upvotes: 1

Views: 1355

Answers (4)

George
George

Reputation: 21

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

Upvotes: 2

Noe7sv
Noe7sv

Reputation: 11

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

Upvotes: 0

mark
mark

Reputation: 21743

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.

Upvotes: 0

liyakat
liyakat

Reputation: 11853

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.

Upvotes: 0

Related Questions