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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ## 驗證器 MixPHP 的驗證器結合了多個框架的優點,如下: - 支持場景控制。 - 驗證成功后字段將賦值為驗證類的屬性,文件則直接實例化為文件對象。 - 更細粒度的錯誤消息設置。 - 支持在 WebSocket 開發中使用。 ## 驗證器定義 我們定義一個 `\apps\index\models\UserForm` 驗證器類用于 `User` 控制器的驗證。 ~~~ <?php namespace apps\index\models; use mix\validators\Validator; class UserForm extends Validator { public $name; public $age; public $email; // 規則 public function rules() { return [ 'name' => ['string', 'maxLength' => 25, 'filter' => ['trim']], 'age' => ['integer', 'unsigned' => true, 'min' => 1, 'max' => 120], 'email' => ['email'], ]; } // 場景 public function scenarios() { return [ 'create' => ['required' => ['name'], 'optional' => ['email', 'age']], ]; } // 消息 public function messages() { return [ 'name.required' => '名稱不能為空.', 'name.maxLength' => '名稱最多不能超過25個字符.', 'age.integer' => '年齡必須是數字.', 'age.unsigned' => '年齡不能為負數.', 'age.min' => '年齡不能小于1.', 'age.max' => '年齡不能大于120.', 'email' => '郵箱格式錯誤.', ]; } } ~~~ >[danger] 如果沒有定義錯誤提示信息,則使用系統默認的提示信息 ## 數據驗證 在需要進行 `User` 驗證的控制器方法中,添加如下代碼即可: ~~~ <?php namespace apps\index\controllers; use apps\index\models\UserForm; use mix\facades\Request; use mix\http\Controller; class UserController extends Controller { public function actionCreate() { app()->response->format = \mix\http\Response::FORMAT_JSON; // 使用模型 $model = new UserForm(); $model->attributes = Request::get() + Request::post(); $model->setScenario('create'); if (!$model->validate()) { return ['code' => 1, 'message' => 'FAILED', 'data' => $model->getErrors()]; } // 執行保存數據庫 // ... // 響應 return ['code' => 0, 'message' => 'OK']; } } ~~~ ## 驗證失敗 驗證失敗可以通過以下方法獲取錯誤消息: - $model->getErrors() :獲取全部錯誤信息,返回數組。 - $model->getError() :獲取單條錯誤信息,返回字符串。 ## 驗證成功 驗證成功后,驗證規則中通過驗證的字段,將會賦值到同名的驗證類的屬性中,未通過的字段則為 `null`。 這個功能有什么用? >[success] 有了這個功能,我們就只需要把真個驗證類的對象傳入模型,就可以在模型里安全的使用這些屬性操作數據庫。
                  <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>

                              哎呀哎呀视频在线观看