<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                > 在開發應用程序時,永遠不要相信從用戶接收到的數據。為了使用模式來驗證用戶的輸入,應該調用 yii\base\Model::validate() 方法。如果驗證成功,它返回一個布爾值。如果有錯誤發生,可以從 yii\base\Model::$errors 得到它們。 [TOC] #### required : 必須值驗證屬性 ~~~ [['字段名'],required,'requiredValue'=>'必填值','message'=>'提示信息'] #說明:CRequiredValidator 的別名, 確保了特性不為空. ~~~ #### email : 郵箱驗證 ~~~ ['email', 'email'] #說明:CEmailValidator的別名,確保了特性的值是一個有效的電郵地址. ~~~ #### match : 正則驗證 ~~~ [['字段名'],match,'pattern'=>'正則表達式','message'=>'提示信息']; 如: ['mobile','match','pattern'=>'/^1\d{10}$/','message'=>'手機號格式不正確'], [['字段名'],match,'not'=>ture,'pattern'=>'正則表達式','message'=>'提示信息'] /*正則取反*/ #說明:CRegularExpressionValidator 的別名, 確保了特性匹配一個正則表達式. ~~~ #### url : 網址 ~~~ ['website', 'url', 'defaultScheme' => 'http']; #說明:CUrlValidator 的別名, 確保了特性是一個有效的路徑. ~~~ #### captcha : 驗證碼 ~~~ ['verificationCode', 'captcha'] #說明:CCaptchaValidator 的別名,確保了特性的值等于 CAPTCHA 顯示出來的驗證碼. ~~~ #### safe : 安全 ~~~ ['description', 'safe'] ~~~ #### compare : 比較 ~~~ ['age', 'compare', 'compareValue' => 30, 'operator' => '>='] #說明:compareValue(比較常量值) - operator(比較操作符) #說明:CCompareValidator 的別名,確保了特性的值等于另一個特性或常量. ~~~ #### default : 默認值 ~~~ ['age', 'default', 'value' => null] #說明:CDefaultValueValidator 的別名, 為特性指派了一個默認值. ~~~ #### exist : 存在 ~~~ [['product_type_id'],'exist','targetClass' => ProductType::className(),'targetAttribute' => 'id','message'=>'請選擇商品分類!'] #說明:CExistValidator 的別名,確保屬性值存在于指定的數據表字段中. ~~~ #### file : 文件 ~~~ ['primaryImage', 'file', 'extensions' => ['png', 'jpg', 'gif'], 'maxSize' => 1024*1024*1024] #說明:CFileValidator 的別名, 確保了特性包含了一個上傳文件的名稱. ~~~ #### filter : 濾鏡 ~~~ [['username', 'email'], 'filter', 'filter' => 'trim', 'skipOnArray' => true] #說明:CFilterValidator 的別名, 使用一個filter轉換屬性. ~~~ #### in : 范圍 ~~~ ['level', 'in', 'range' => [1, 2, 3]] #說明:CRangeValidator 的別名,確保了特性出現在一個預訂的值列表里. ~~~ #### unique : 唯一性 ~~~ ['username', 'unique'] #說明:CUniqueValidator 的別名,確保了特性在數據表字段中是唯一的. ~~~ #### integer : 整數 ~~~ ['age', 'integer'] ~~~ #### number : 數字 ~~~ ['salary', 'number'] ~~~ #### double : 雙精度浮點型 ~~~ ['salary', 'double'] ~~~ #### date : 日期 ~~~ [['from', 'to'], 'date'] ~~~ #### string : 字符串 ~~~ ['username', 'string', 'length' => [4, 24]] ~~~ #### boolean : 是否為一個布爾值 ~~~ ['字段名', 'boolean', 'trueValue' => true, 'falseValue' => false, 'strict' => true] #說明:CBooleanValidator 的別名 ~~~ #### image :是否為有效的圖片文件 ~~~ ['primaryImage','image', 'extensions' => 'png, jpg,jpeg','minWidth' => 100,'maxWidth' => 1000,'minHeight' => 100,'maxHeight' => 1000,] ~~~ #### custom:自定義驗證 ~~~ ['mobile', 'checkMobile'] //驗證手機號碼 public function checkMobile(){ $reg = '/^1\d{10}$/'; if($this->mobile && !preg_match($reg, $this->mobile)){ $this->addError('mobile', Yii::t('common','Mobile Is Invalid')); return false; } } //也可以用正則驗證器替代 ['mobile','match','pattern'=>'/^1\d{10}$/','message'=>'手機號格式不正確'], ~~~ #### 修改驗證器默認message英文提示 ~~~ [['email','username','pwd','nickname'],'required'] //當使用這樣的驗證規則,如果我們要對其進行每個字段進行message提示的時候,就得拆分開,單獨設置各自的message,這樣寫起來復雜而又費事。最簡單的方式就是寫成以下這樣 [['email','username','pwd','nickname'],'required','message'=>'{attribute} 不能為空!'] //當然,如果還想再簡單一點,就直接修改\vendor\yiisoft\yii2\validators\下各自的驗證器,在各自的init方法中把英文的提示改為中文的就可以了,我覺得這樣的方式是最方面省事的。仁者見仁智者見智吧。 ~~~
                  <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>

                              哎呀哎呀视频在线观看