簡體   English   中英

模型驗證中的Phalcon可選字段

[英]Phalcon optional fields in model validation

我已經驗證了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
                )
            )
        );

當價格字段為空時,它將不會驗證。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM