如何覆盖 Phalcon-devtools 生成的模型的 Not Null 项的错误消息?

How to overwrite error messages for Not Null items of Model generated by Phalcon-devtools?

问题

PresenceOf 的验证规则是为在 Phalcon-devtools 生成的模型的 table 定义中设置了 Not Null 约束的项目自动设置的,但与消息“name is required”出现错误时会自动完成。

您不必更改 PresenceOf 的验证规则,但请教我如何覆盖此错误消息。


概览


源代码

<?php

use Phalcon\Mvc\Controller;
use Phalcon\Validation;
use Phalcon\Validation\Validator\PresenceOf;

class User extends ModelBase
{
    /**
     *
     * @var string
     * @Column(type="string", length=767, nullable=false)
     */
    public $name;

    public function validation()
    {
        $validator = new Validation();

        $validator->add(
            'name',
            new PresenceOf([
                'message' => "required",
            ])
        );

        return $this->validate($validator);
    }

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->setSchema("lashca");
        $this->setSource("user");
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'user';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return User[]|User|\Phalcon\Mvc\Model\ResultSetInterface
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return User|\Phalcon\Mvc\Model\ResultInterface
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

}

环境

您可以尝试将默认值直接设置到您的模型变量中

 /**
 *
 * @var string
 * @Column(type="string", length=767, nullable=false)
 */
public $name = '';

您可以做的另一件事是直接从数据库设置默认值并将 'default' RawValue 作为值

传递
protected function beforeValidationOnCreate() {
    if (empty($this->name)) {
        $this->name = new \Phalcon\Db\RawValue('default');
    }
}

您终于可以禁用默认的 phalcon 验证了

 public function initialize() {
    $this->setup(array('notNullValidations' => false));
}