使用方法

// 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 app\models;

use yii\base\Model;

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

YII2之 Scenario的更多相关文章

  1. Yii2 场景scenario的应用

    首先,我们在使用模型类中的验证,rules的时候,会出现以下情况: 假设有一个字段type, 当type

  2. YII2 model 字段验证提示 Unknown scenario: update

    意思是 update 场景不存在,也就是 定义的 rules 中没有该规则: /** * @inheritdoc * 验证规则 */ public function rules() { return ...

  3. Yii2 认证实现原理和示例

    Yii的用户认证分为两个部分,一个是User组件,负责管理用户认证状态的,包括登录,登出,检测当前登录状态等,源文件位于vender/yiisoft/yii2/web/User.php.另一个是实现接 ...

  4. Yii2 事务操作

    官网关于Yii2 事务的说明文档 http://www.yiiframework.com/doc-2.0/guide-db-active-record.html Working with Transa ...

  5. Yii2 用户登录

    在Yii2的basic版本中默认是从一个数组验证用户名和密码,如何改为从数据表中查询验证呢?且数据库的密码要为哈希加密密码验证? 下面我们就一步一步解析Yii2的登录过程. 一. 创建user表模型 ...

  6. yii2开发后记

    h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h ...

  7. [moka同学摘录]Yii2.0开发初学者必看

    想要了解更多YII,PHP方面内容,请关注本博客. 基础总结 1.修改默认控制器/方法 yii默认是site控制器,可以在web.php中设置$config中的'defaultRoute'='xxxx ...

  8. yii2源码学习笔记(六)

    Behvaior类,Behavior类是所有事件类的基类: 目录yii2\base\Behavior.php <?php /** * @link http://www.yiiframework. ...

  9. Yii2.0中场景的使用小记

    熟悉Yii框架的人都知道,灵活的使用场景可以达到事半功倍的效果! 比如普通的数据的新增.修改,新增需要验证其中两个字段,而修改只需要验证其中一个字段:还有种情况,也是我们现在用到的,同一张表(同一个m ...

随机推荐

  1. [LeetCode] Missing Number 丢失的数字

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  2. [LeetCode] Shortest Word Distance 最短单词距离

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  3. [LeetCode] N-Queens II N皇后问题之二

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  4. oracle数据泵导入

    假设将dmp放到/data目录下,首先在数据库中创建directory目录SQL> create directory exp as '/data/'在操作系统命令执行导入命令.impdp sys ...

  5. replace U to T in mature.fa

    sed '2~2s/U/T/g' mature.fa > miRBase_mature.fa

  6. py-faster-rcnn之从solver文件创建solver对象,建立pythonlayer

    faster-rcnn在训练阶段,根据一个solver的prototxt文件创建相应的网络.仅凭一个prototxt就创建网络?其实还涉及到自定义的PythonLayer. 比如lib/rpn/anc ...

  7. SUSE系统查看各种信息

    系统版本 # 显示内核版本 uname -a # 可看gcc版本 cat /proc/version # 显示linux基准库 lsb_release -a # 显示Suse版本准确版本 cat /e ...

  8. CUDA[2] Hello,World

    Section 0:Hello,World 这次我们亲自尝试一下如何用粗(CU)大(DA)写程序 CUDA最新版本是7.5,然而即使是最新版本也不兼容VS2015 ...推荐使用VS2012 进入VS ...

  9. windows下mysql数据库定时备份。

    注意:看本教程先必须会windows自带的"任务计划程序". 首先创建一个bat后缀的文件我的是timerExecutePhp.bat文件 timerExecutePhp.bat ...

  10. js事件(Event)知识整理

    事件(Event)知识整理,本文由网上资料整理而来,需要的朋友可以参考下   鼠标事件 鼠标移动到目标元素上的那一刻,首先触发mouseover 之后如果光标继续在元素上移动,则不断触发mousemo ...