andreshernandez
andreshernandez

Reputation: 226

Is there an equivalent to 'minYear'/'maxYear' for months in a date input in CakePHP?

I would like to limit the dates available to those in this and next month.

So far, I know I can specify a minimum and maximum year, which is part of what I need.

$this_month = new DateTime('now');
$next_month = new DateTime('next month');

echo $this->Form->input('date', 
    array(
        'minYear'  => $this_month->format('Y'),
        'maxYear'  => $next_month->format('Y'),
    )
);

Is there a similar way in which I can specify the months I need?

Upvotes: 1

Views: 928

Answers (1)

Justin Yost
Justin Yost

Reputation: 2340

Two ways of solving this problem neither is as simple as you would probably like:

First:

Create a class that extends the FormHelper class and create your own month function that adds the ability for this.

Second:

You can pass in an array of month names, so you could generate only the list of months that you wish an end user to select and pass that into your month function:

<?php echo $this->Form->month('months', null, array('monthNames' => array(1 => 'Jan', 2 => 'Feb', 3 => 'March'))); ?>

Will generate a month selector with only those three months in the drop down.

Upvotes: 1

Related Questions