search
Homephp教程PHP开发Detailed explanation of Yii2.0 scenarios scenario application

Yii2.0 scenario usage:

For a simple example, there are three fields: title, image, content in the post table. When I create a post, I want all three fields It is a required field, but when you modify it, the two fields of title content are required, and iamge does not need to be filled in. Under normal circumstances, [['title', 'content', 'image'], 'required',], but when we change it, we only need [['title', 'content'], 'required'], just OK, but without the image field, our form cannot be submitted. What should we do about this problem? ?

Scenarios can help you solve this problem. The following is a simple scenario example.

1. First, we define the scene class name in the model and it must be scenarios()

public function scenarios()
{
return [
'create' => ['title', 'image', 'content'],
'update' => ['title', 'content'],
];
}

2. Okay, as shown above, we have completed part of the basic setting of the scene. Next we set rules(), and when calling the scene, we use the on keyword

[['title', 'content'], 'required', 'on' => ['create', 'update']],
[['image'], 'required', 'on' => 'create'],
[['image'], 'image', 'enableClientValidation' => true, 'maxSize' => 1024, 'message' => '您上传的文件过大', 'on' => ['create', 'update']],

on 指定的就是场景,一个场景用字符串,多个场景用数组

3. Okay, we have set up the model. Now let’s call it.

$model = $this->findModel($id);
$model->setScenario('update'); 
//或者 $model->scenario = 'update'; 都可以

in the Controller. The above means Call the update scenario. Everything is that simple

The above is the detailed explanation of Yii2.0 scenarios scenario application. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools