• YII2之 Scenario


     使用方法

    // scenario is set as a property
    $model = new User;
    $model->scenario = User::SCENARIO_SHOW;
    
    // scenario is set through configuration
    $model = new User(['scenario' => User::SCENARIO_SUBMIT]);

    举例说明

    <?php
    
    
    namespace appmodels;
    
    use yiiaseModel;
    
    class ContactForm extends Model
    {
        public $username;
        public $password;
        public $email;
    
        const SCENARIO_SHOW = 'show';
        const SCENARIO_SUBMIT = 'submit';
    
        public function scenarios()
        {
            return [
                self::SCENARIO_SHOW => ['username', 'password'],
                self::SCENARIO_SUBMIT => ['username', 'email', 'password'],
            ];
        }
    
        public function rules()
        {
            return [
                // username, email and password are all required in "submit" scenario
                [['username', 'email', 'password'], 'required', 'on' => self::SCENARIO_SUBMIT],
    
                // username and password are required in "show" scenario
                [['username', 'password'], 'required', 'on' => self::SCENARIO_SHOW],
            ];
        }
        
        public function attributeLabels()
        {
    
            switch($this->scenario)
            {
                case self::SCENARIO_SHOW:
                    $labels = [
                        'username' => 'User Name',
                        'password' => 'Password',
                    ];
                    break;
    
                case self::SCENARIO_SUBMIT:
                    $labels = [
                        'username' => 'User Name',
                        'password' => 'Password',
                        'email' => 'Your email address',
                    ];
                    break;
            }
            return $labels;
        }
    }
  • 相关阅读:
    解决sqlite3 dos下显示中文乱码
    毕业两年
    成就感
    重构html的下拉框select
    ie6 select不兼容处理(转)
    全选删除确认改进
    GridView移动行变色
    gridview固定列的宽度并且能换行
    分页控件结合分页存储过程
    网页滚动条向下拉动奇慢的原因
  • 原文地址:https://www.cnblogs.com/shaoyikai/p/5012190.html
Copyright © 2020-2023  润新知