我已经验证了 Phalcon 模型中的一些字段,如下所示
class Ads extends Phalcon\Mvc\Collection
{
public function validation()
{
$this->validate(
new InclusionIn(
array(
"field" => "type",
"message" => "Type must be: mechanical or virtual",
"domain" => array("Mechanical", "Virtual")
)
)
);
$this->validate(
new Numericality(
array(
"field" => "price",
"message" => "Price must be numeric"
)
)
);
return $this->validationHasFailed() != true;
}
}
如何在验证中将某些字段定义为可选字段,将某些字段定义为必填字段?
可选字段:
例如,当价格存在时,验证它,当不存在价格时,忽略它。
必填字段:
当价格不存在时,不向数据库插入数据并返回相关错误信息。
最佳答案
使用allowEmpty
作为
$this->validate(
new Numericality(
array(
"field" => "price",
"message" => "Price must be numeric",
"allowEmpty" => true
)
)
);
当价格字段为空时,不会生效。
关于php - 模型验证中的 Phalcon 可选字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35813003/