如何覆盖 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
的验证规则,但请教我如何覆盖此错误消息。
概览
使用以下命令使用 Phalcon-devtools 生成模型。
phalcon model user
即使不特别设置校验规则,如果对Not Null的输入项name
不输入就保存,也会出错,可以获取消息“需要姓名”。
要使错误消息成为您自己的字符串,请将以下内容添加到用户模型中。
$validator = new Validation();
$validator->add(
'name',
new PresenceOf([
'message' => "required",
])
);
假设获取“required”作为错误信息,但结果没有改变它得到“需要名称”。
源代码
<?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);
}
}
环境
- CentOS 7.4(x64)
- 阿帕奇 2.4.6
- PHP 7.0.26
- Phalcon 框架 3.2.4
- MySQL 版本 14.14 分发版 5.6.38
您可以尝试将默认值直接设置到您的模型变量中
/**
*
* @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));
}
问题
PresenceOf
的验证规则是为在 Phalcon-devtools 生成的模型的 table 定义中设置了 Not Null 约束的项目自动设置的,但与消息“name is required”出现错误时会自动完成。
您不必更改 PresenceOf
的验证规则,但请教我如何覆盖此错误消息。
概览
使用以下命令使用 Phalcon-devtools 生成模型。
phalcon model user
即使不特别设置校验规则,如果对Not Null的输入项
name
不输入就保存,也会出错,可以获取消息“需要姓名”。要使错误消息成为您自己的字符串,请将以下内容添加到用户模型中。
$validator = new Validation(); $validator->add( 'name', new PresenceOf([ 'message' => "required", ]) );
假设获取“required”作为错误信息,但结果没有改变它得到“需要名称”。
源代码
<?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);
}
}
环境
- CentOS 7.4(x64)
- 阿帕奇 2.4.6
- PHP 7.0.26
- Phalcon 框架 3.2.4
- MySQL 版本 14.14 分发版 5.6.38
您可以尝试将默认值直接设置到您的模型变量中
/**
*
* @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));
}