CakePHP在空字段上显示自定义验证消息


CakePHP show custom validation message on empty fields

我正在搜索一种方法,在notEmpty规则中显示验证数组上声明的自定义消息。所以如果我有这个验证:

'username' => array(
        'notEmpty' => array(
            'rule' => 'notEmpty',
            'message' => 'username empty',
            'required' => true
        ),
        //other validation rules

如果我在表单中保留e字段为空,cakephp会显示他自己的空字段默认消息,而不是我的自定义消息。如何显示它并将此事件捕获到我的Controller类中?

数组的键应该是字段名,除非我误解了您的上下文:

'myFieldName' => array(
           'rule' => 'notEmpty',
           'message' => 'custom message for empty field',
           'required' => true
       )

如果我误解了上下文,请编辑你的问题,对你的模型中的验证代码有一个更大的了解。