Php 模型验证中的可选字段
我已经验证了Phalcon模型中的一些字段,如下所示Php 模型验证中的可选字段,php,phalcon,Php,Phalcon,我已经验证了Phalcon模型中的一些字段,如下所示 class Ads extends Phalcon\Mvc\Collection { public function validation() { $this->validate( new InclusionIn( array( "field" => "type",
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
as
$this->validate(
new Numericality(
array(
"field" => "price",
"message" => "Price must be numeric",
"allowEmpty" => true
)
)
);
当价格字段为空时,它将不会验证。这有点误导,因为
allowEmpty
对于0
作为值也是如此。