执行“大规模分配”时是否执行验证?在伊?

发布于 2025-01-04 05:49:00 字数 1022 浏览 0 评论 0原文

我读过一篇文章 “了解“安全”验证规则”< /a>

作者说:

关键点 - 大规模分配只会针对具有以下特征的字段: 通过了一些明确的验证规则。明显的“实际”验证器 - 长度、电子邮件、必填等 - 全部符合条件,但某些字段是自由格式和可选的,并且没有任何格式要求 - 用户可以输入任何他喜欢的内容,包括将其留空。

但有一个评论

我承认我没有花时间彻底确认这一点,但正如 据我记得,您将验证集中在中是不正确的 与作业。如果内存运行正常,当你做大量的事情时 赋值 ($model->setAttributes($_POST['Model'])),没有实际的 进行验证。相反,对于 POST/GET 中的每个属性 数组,模型检查该属性是否可以安全分配 (通过检查是否有验证或安全规则 属性,而不实际验证它) - 如果有则设置 属性,如果不是,则忽略它。

仅当您调用 $model->save() (或 明确 $model->validate()),当属性已经 使用验证规则检查已分配的内容。

还值得一提的是,大量作业和个人作业 分配实际上并不等同 - 大量分配检查 每个属性是否有验证或安全规则,而 如果您进行单独分配($model->attribute = $_POST['Model']['attribute'];) 没有进行这样的检查 - Yii 假设 该来源是可信的。

那么,任何人都可以证明哪一个是正确的吗?

I've read an article "Understanding "Safe" Validation Rules"

the author said that:

Key Point - Massive Assignment will only be made for fields which have
passed some explicit validation rule. The obvious "actual" validators
- length, email, required, etc. - all qualify, but some fields are free form and optional, and don't have any format requirements - the
user can put whatever he likes, including leaving it blank.

But there is a comment:

I admit I haven't taken the time to confirm this thoroughly, but as
far as I remember, you are not correct in lumping the validation in
with the assignment. If memory serves correctly, when you do massive
assignment ($model->setAttributes($_POST['Model'])), no actual
validation takes place. Rather, for each attribute in the POST/GET
array, the model checks whether that attribute is safe for assignment
(by checking whether there is a validation or safe rule for that
attribute, without actually validating it) - if there is then it sets
the attribute, if not it ignores it.

Validation only takes place when you call $model->save() (or
explicitly $model->validate()), when the attributes that have already
been assigned are checked using the validation rules.

It is also worth mentioning that massive assignment and individual
assignment are not actually equivalent - massive assignment checks
whether there is a validation or safe rule for each attribute, whereas
if you make an individual assignment ($model->attribute =
$_POST['Model']['attribute'];) no such check is made - Yii assumes
that the attribution is trusted.

So, any one can show which is the right thing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅唱ヾ落雨殇 2025-01-11 05:49:00

只有当字段具有验证规则且其类型不“不安全”时,才可以进行大规模赋值。然而,它并未在大规模分配时得到验证。

当您调用 validate() 或调用 save(true) 时,它会被验证。

不要忘记您可以直接在其 Class Reference 中阅读 Yii 源代码

A field can be massively assigned only if it has a validation rule, whose type is not "unsafe". It is not validated upon massive assignment, however.

It is validated when you call validate() or call save(true).

Don't forget that you can read the Yii source code directly in their Class Reference

抽个烟儿 2025-01-11 05:49:00

大规模分配发生在两种情况下 -
1. 当您为字段设置验证规则时。在这种情况下,该字段将经过验证。
2. 当您声明该字段为安全字段时。在这种情况下,您可能有也可能没有字段的验证规则。无论如何,任务都会完成。

因此,正确的答案是 - “取决于”您是否为该字段定义了验证规则。

Massive assignment happens in two cases -
1. When you set a validation rule for a field. In that case the field goes through validation.
2. When you declare the field as safe. In this case, you might or might not have a validation rule for a field. The assignment will go through irrespective.

So the correct answer is - 'depends' on if you have a validation rule defined for the field or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文