简体   繁体   English

模型验证中的Phalcon可选字段

[英]Phalcon optional fields in model validation

i have validated some fields in Phalcon model like below 我已经验证了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;
    }

}

How can i define some fields as optional fields and some fields as required fields in validation? 如何在验证中将某些字段定义为可选字段并将某些字段定义为必填字段?

optional fields: 可选字段:
for example when price exist, validate it, when not price, ignore it. 例如,当价格存在时,对其进行验证;当价格不存在时,对其进行忽略。

required fields : 必填字段:
when price not exist, don't insert data into database and return related error message. 当价格不存在时,请勿将数据插入数据库并返回相关错误消息。

use allowEmpty as 使用allowEmpty作为

$this->validate(
            new Numericality(
                array(
                    "field"   => "price",
                    "message" => "Price must be numeric",
                    "allowEmpty" => true
                )
            )
        );

When the price field is empty, it will not validate. 当价格字段为空时,它将不会验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM