如何在 CakePHP 表单中设置最小或最大日期限制?

How to set min or max date limit in CakePHP form?

在我当前的日期表单输入中,我有参数:

'maxYear' => date('Y')

这让我可以将最大年份限制为今年,但我还需要限制日期 selection 以便用户无法 select 将来的日期。使用 maxYear 只允许限制年份,但我还需要月份和日期

这不是您可以使用表单助手(或 HTML 表单选项,即使您手动编写它们)也能做到的事情。


问题:

如果您将月份限制为 1-7(如果我们目前在 7 月),则用户将无法 select 任何这些月份的前一年。


解决方法:

如果您使用 jQuery)

,请使用 JavaScript onchange validation. When the field(s) change date, check the selected date against your required date range, and give notification if it fails. (see jQuery's .change() documentation

(然后在back-end上验证,当然,因为JavaScript很容易被操纵)