<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國際加速解決方案。 廣告
                ## Yii2模型中的場景 [TOC]   在不同的場景下,模型可能會使用不同的業務規則和邏輯, 例如`email`屬性在注冊時強制要求有,但在登陸時不需要;也就是說`User`模型可能會在收集用戶登錄輸入, 也可能會在用戶注冊時使用驗證。   場景特性主要在**驗證**、**屬性塊賦值**或者**基于不同的場景定義不同的 屬性標簽**。 ### 使用場景進行驗證 #### 定義模型驗證規則 文件在`app\models\Users.php`內容如下: ```php <?php namespace app\models; use yii\db\ActiveRecord; class Users extends ActiveRecord { const SCENARIO_LOGIN = 'login'; const SCENARIO_REGISTER = 'register'; /** * @return string */ public static function tableName() { return 'users'; } public function rules() { return [ // 在"register" 場景下 username, email 和 password 必須有值 [['username', 'email', 'password'], 'required', 'on' => self::SCENARIO_REGISTER], // 在 "login" 場景下 username 和 password 必須有值 [['username', 'password'], 'required', 'on' => self::SCENARIO_LOGIN], ]; } } ``` #### 在控制器中使用 文件在`app\controllers\UserController.php`內容如下: ```php <?php namespace app\controllers; use app\models\Users; use yii\web\Controller; class UserController extends Controller { public function actionLogin() { $model = new Users; $model->scenario = Users::SCENARIO_LOGIN; // 或者通過構造函數配置 $model = new Users(['scenario'=>'login']); if (\Yii::$app->request->isPost) { } return $this->render('login', ['model' => $model]); } public function actionRegister() { $model = new Users(['scenario'=>'register']); if (\Yii::$app->request->isPost) { } return $this->render('login', ['model' => $model]); } } ``` #### 視圖文件 文件在`app\views\user\login.php`內容如下: ```html <?php use yii\helpers\Html; use yii\widgets\ActiveForm; ?> <?php $form = ActiveForm::begin(['id' => 'contact-form']); ?> <?= $form->field($model, 'username')->textInput(['autofocus' => true]) ?> <?= $form->field($model, 'email') ?> <?= $form->field($model, 'password')->passwordInput() ?> <div class="form-group"> <?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'contact-button']) ?> </div> <?php ActiveForm::end(); ?> ``` #### 訪問預覽 ![](https://box.kancloud.cn/898ad73c868acd446f765ff8a901539a_1223x446.png) > 不同的控制器調用不同的**模型場景**,對數據進行校驗。 ### 使用場景進行屬性塊賦值   使用場景進行屬性塊賦值只是在賦值給模塊的`attributes`屬性賦值的時候會根據定義的規則進行賦值。 #### 定義模型場景規則 文件在`app\models\Users.php`內容如下: ```php <?php namespace app\models; use yii\db\ActiveRecord; class Users extends ActiveRecord { const SCENARIO_LOGIN = 'login'; const SCENARIO_REGISTER = 'register'; /** * @return string */ public static function tableName() { return 'users'; } /** * @return array */ public function scenarios() { $scenarios = parent::scenarios(); $scenarios[self::SCENARIO_LOGIN] = ['username', 'password']; $scenarios[self::SCENARIO_REGISTER] = ['username', 'email', 'password']; return $scenarios; } } ``` #### 控制器代碼 ```php <?php namespace app\controllers; use app\models\Users; use yii\web\Controller; class UserController extends Controller { public function actionLogin() { $model = new Users; $model->scenario = Users::SCENARIO_LOGIN; // 或者通過構造函數配置 $model = new Users(['scenario'=>'login']); if (\Yii::$app->request->isPost) { $model->attributes = \Yii::$app->request->post('Users'); print_r($model); // 查看model的屬性只有"username"和"password"被賦值 } return $this->render('login', ['model' => $model]); } public function actionRegister() { $model = new Users(['scenario'=>'register']); if (\Yii::$app->request->isPost) { $model->attributes = \Yii::$app->request->post('Users'); print_r($model);// 查看model的屬性只有"username","email"和"password"被賦值 } return $this->render('login', ['model' => $model]); } } ``` #### 查看視圖結果 ![](https://box.kancloud.cn/4af648836112abbfd1b8112695e4fec4_1269x226.png) > 訪問`login()`操作,得到的是模型中定義的`login`場景限制的字段'username', 'password'。 訪問`register()`操作,得到的是模型中定義的`register`場景限制的字段'username', 'email', 'password'。 ### 使用場景定義不同的屬性標簽   屬性標簽是 視圖一部分,但是在模型中申明標簽通常非常方便, 并可形成非常簡潔可重用代碼。 #### 定義模型規則 文件在`app\models\Users.php`內容如下(主要查看`attributeLabels()`方法): ```php <?php namespace app\models; use yii\db\ActiveRecord; class Users extends ActiveRecord { const SCENARIO_LOGIN = 'login'; const SCENARIO_REGISTER = 'register'; /** * @return string */ public static function tableName() { return 'users'; } /** * @return array */ public function attributeLabels() { if ($this->scenario == self::SCENARIO_LOGIN) { $typeString = '登錄'; $userName = $typeString . '用戶名'; $email = $typeString . '郵箱'; $password = $typeString . '密碼'; } else { $typeString = '注冊'; $userName = $typeString . '名'; $email = $typeString . '郵箱'; $password = $typeString . '密碼'; } return [ 'username' => $userName, 'email' => $email, 'password' => $password, ]; } } ``` #### 控制器使用`render()`方法渲染模板文件 ```php <?php namespace app\controllers; use yii\web\Controller; class UserController extends Controller { public function actionLogin() { return $this->render('login', ['model' => $model]); } public function actionRegister() { return $this->render('login', ['model' => $model]); } } ``` #### 查看效果 ![](https://box.kancloud.cn/e4b9be880bb6c5c785291575e64bc8b7_1594x382.png) > 訪問`register()`操作和`login()`操作相應的提示字段變成自定義的。
                  <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>

                              哎呀哎呀视频在线观看