问题
PresenceOf的验证规则是为Phalcon-devtools生成的模型的表定义中设置了非空约束的项自动设置的,但是在错误发生时,当消息“名称是必需的”时,它将自动完成。
您不必更改PresenceOf的验证规则,但请教我如何覆盖此错误消息。
概述
name,则会发生错误,并且您可以获得消息"name是必需的“。源代码
<?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);
}
}环境
发布于 2017-12-01 03:25:18
您可以尝试将默认值直接设置到模型变量中。
/**
*
* @var string
* @Column(type="string", length=767, nullable=false)
*/
public $name = '';您可以做的另一件事是直接从数据库中设置默认值,并将“默认”RawValue作为值传递。
protected function beforeValidationOnCreate() {
if (empty($this->name)) {
$this->name = new \Phalcon\Db\RawValue('default');
}
}最后,可以禁用默认的phalcon验证。
public function initialize() {
$this->setup(array('notNullValidations' => false));
}https://stackoverflow.com/questions/47562229
复制相似问题