<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] # 自定義驗證器 除了使用那些自帶的驗證規則以外,如果它們不能滿足你的校驗需求,還可以自己定義驗證器: ~~~php public function rules(){ return [ //驗證規則是一個方法名稱,這個方法指向當前模型的一個public方法 ['username', 'myMethod'] ]; } public function myMethod(){ if(strpos($this->username, 'fuck!') !== false){ $errorFlag = 'username'; //隨便,一般用被校驗的屬性名,比如 username $this->addError($errorFlag, '用戶名稱出現了敏感詞匯!'); } } ~~~ 然后validate的時候就會自動調用myMethod方法來按照你的邏輯去校驗username了 當校驗不成功的時候就調用addError來添加一個錯誤消息,這樣的話外面就能通過`errors`或`firstErrors`屬性來取得錯誤消息了 # each驗證器 如果要驗證的屬性是一個數組,希望里面每一個元素都必須是整數的話,可以用這個驗證器 ~~~php public function rules(){ return [ ['deleteIds', 'each', 'rule' => ['integer']] ]; } ~~~ each就是要求每一個元素都符合rule的要求,而rule的書寫方式又是一個數組,里面第一個元素是驗證器別名,其它用key來定義這個驗證器的更多內容就是了(你就理解為驗證器嵌套吧),比如 ~~~php public function rules(){ return [ ['deleteIds', 'each', 'rule' => ['integer', 'min' => 1]] ]; } ~~~ 和平時使用驗證規則一樣是相通的,知道integer是指[yii\\validators\\NumberValidator](http://www.yiichina.com/doc/api/2.0/yii-validators-numbervalidator)后,找到里面的屬性$min再定義到這個rule里進行屬性注入即可 # exist驗證器 經常用于校驗一個ID屬性什么的是否在數據庫里存在,比如這樣: ~~~php public function rules(){ return [ ['id', 'exist', 'targetClass' => 'app\models\User'] ]; } ~~~ `targetClass`指定了**目標類**,目標類必須是一個[AR模型](http://www.kkh86.com/it/yii2/guide-db-ar-base.html),然后exist驗證器就會去查一下AR模型對應的表中有沒有`WHERE id = $表單->id`的記錄,有的話就認為校驗通過 # 當表單模型屬性與AR模型表字段屬性命名不一致時的存在性校驗 假設有一個收發消息的功能,消息記錄表有個**sender\_id**(發送者)和**receiver\_id**(接收者),表單模型要往user表中校驗id字段,但表單模型是這樣定義屬性的: ~~~php public $senderId = 0; public $receiverId = 0; ~~~ 如果這樣定義規則: ~~~php public function rules(){ return [ ['senderId', 'exist', 'targetClass' => 'app\models\User'], ['receiverId', 'exist', 'targetClass' => 'app\models\User'], ]; } ~~~ 就會造成驗證器去找user表的senderId字段和receiverId字段,但并沒有這兩個字段啊,查詢就會出錯 實際上我們希望找id字段來確定用戶是否存在,可以通過`targetAttribute`來指定模型的哪個屬性(字段) ~~~php public function rules(){ return [ ['senderId', 'exist', 'targetClass' => 'app\models\User', 'targetAttribute' => 'id'], ['receiverId', 'exist', 'targetClass' => 'app\models\User', 'targetAttribute' => 'id'], ]; } ~~~ 如果有更多需求,詳細用法請見官方文檔 # unique 唯一驗證器 這個通常用來校驗注冊郵箱或手機號什么的 ~~~php public function rules(){ return [ ['email', 'unique', 'targetClass' => 'app\models\User'], ]; } ~~~ 就是校驗user表的email字段是否有相同的值,有就不通過,沒有就可以;也能像exist驗證器那樣通過targetAttribute來指定要校驗的字段 # 預處理 一些string類型的輸入參數我們通常都希望提前trim一下空格,不然也會成功通過string的length校驗(不會自動trim) 那難道先在控制器里取值,trim掉再傳給表單模型嗎?——不用,直接用trim過濾器就行 ~~~php public function rules(){ return [ [['username', 'email'], 'trim'], ['username', 'string', 'length' => [2, 10]], ['email', 'email'], ]; } ~~~ # 默認值 有些字段不是required的,或者有默認值的,方法1是在類里面聲明的時候就定義默認值:`public $isDelete = 1` 要不就在規則里定義默認值: ~~~php public function rules(){ return [ //如果這樣我寧愿默認在屬性定義里,做人不能太死腦筋吧 ['isDelete', 'default', 'value' => 1], //支持動態計算默認值 [['from', 'to'], 'default', 'value' => function ($model, $attribute) { return date('Y-m-d', strtotime($attribute === 'to' ? '+3 days' : '+6 days')); }], ]; } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看