【问题标题】:How to validate the date input in the form for cakephp?如何验证 cakephp 表单中输入的日期?
【发布时间】:2019-03-08 10:30:55
【问题描述】:

如果该日期已经存在于任何其他课程,我想限制允许用户选择的日期。

$validator
            ->date('lesson_date')
            ->requirePresence('lesson_date', 'create')
            ->notEmpty('lesson_date');

这是 Model/Table/LessonsTable.php 中的验证器。但我希望能够检查课表,看看是否有任何其他课程具有相同的日期。如果没有,则可以选择该日期,否则提示选择下周的同一天

【问题讨论】:

    标签: cakephp-3.0


    【解决方案1】:

    验证规则仅限于实体本身的数据。
    如果要将实体与上下文进行比较,则需要使用构建规则。如果看起来像:

    class Table
    {
        // ...
    
        function buildRules($rules)
        {
            // Make sure the date is unique
            $rules->add($rules->isUnique(['date']));
        }
    
        // ...
    }
    

    另见

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多