如何在 yii2 的插入验证器上使用 required ? [英] how to use required on insert validator for yii2?

查看:31
本文介绍了如何在 yii2 的插入验证器上使用 required ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以对如何使用 "on" => 提出建议吗?yii2 中的插入" 验证?我用过

Can anyone have a suggestion about how to use "on" => "insert" validation in yii2? I have used

array('field_name', 'required', 'on'=>'insert')

Yii1 但在 yii2 它不检查我是否设置

in Yii1 but in yii2 it does not check if I set

['field_name', 'required', 'on'=>'insert']

.这里有什么问题你能解释一下吗?提前致谢.

. What is the problem here can you please explain? Thanks in advance.

推荐答案

您应该在验证之前简单地设置模型的场景:

You should simply set your model's scenario before validation :

$model->scenario = 'insert';

从 Yii2 开始:

默认情况下,一个模型只支持一个名为 default

By default, a model supports only a single scenario named default

阅读更多:http://www.yiiframework.com/doc-2.0/指南结构模型.html#scenarios

这篇关于如何在 yii2 的插入验证器上使用 required ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆