<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國際加速解決方案。 廣告
                [TOC] # 簡介 在不同的場景下,模型可能會使用不同的業務規則和邏輯, 例如`email`屬性在注冊時強制要求有,但在登陸時不需要;也就是說`User`模型可能會在收集用戶登錄輸入, 也可能會在用戶注冊時使用驗證。   場景特性主要在**驗證**、**屬性塊賦值**或者**基于不同的場景定義不同的 屬性標簽**。 # 使用場景進行驗證 ## 定義模型驗證規則 文件在`app\models\Users.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 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]); } } ~~~ # 使用場景進行屬性塊賦值 使用場景進行屬性塊賦值只是在賦值給模塊的`attributes`屬性賦值的時候會根據定義的規則進行賦值。 ## 定義模型場景規則 文件在`app\models\Users.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 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]); } } ~~~ # 使用場景定義不同的屬性標簽 屬性標簽是 視圖一部分,但是在模型中申明標簽通常非常方便, 并可形成非常簡潔可重用代碼。 ## 定義模型規則 文件在`app\models\Users.php`內容如下(主要查看`attributeLabels()`方法) ~~~ <?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 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]); } } ~~~
                  <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>

                              哎呀哎呀视频在线观看