简体   繁体   English

Yii2:有条件的安全验证器

[英]Yii2: safe validator on condition

I am trying to use this validation rule in my model, but it's not working. 我正在尝试在模型中使用此验证规则,但是它不起作用。

I mean it always remains safe even if select other option. 我的意思是,即使选择其他选项,它始终保持安全。

[['dhanwantri_bill_number'], 'safe', 
    'when' => function($model) {
        return $model->bill_type =='d';                 
    },
    'whenClient' => "function (attribute, value) {
                         return $('#opdtestbill-bill_type').val() == 'd';
                     }"
],

Am I doing anything wrong? 我做错什么了吗? is there any alternative solution to achieve the same. 有没有其他解决方案可以达到相同的目的?

Thanks. 谢谢。

Rule for bill_type is like bill_type规则就像

[['bill_type'], 'string', 'max' => 20],
[['bill_type','test_name','date'], 'required'],

Edit safe attribute public properties as per doc 根据文档编辑安全属性的公共属性

$when - callable - A PHP callable whose return value determines whether this validator should be applied. $ when-可调用-PHP可调用,其返回值确定是否应应用此验证器。 yii\\validators\\Validator yii \\ validators \\ Validator

$whenClient - string - A JavaScript function name whose return value determines whether this validator should be applied on the client side. $ whenClient-字符串-一个JavaScript函数名称,其返回值确定是否将此验证器应用于客户端。 yii\\validators\\Validator yii \\ validators \\ Validator

As the 'safe' validator just tells that an attribute may be set by massive assignment, the approach is not appropiate. 由于“安全”验证器仅告诉您可以通过大量分配来设置属性,因此该方法不合适。 It just says that when you use the load() method the attribute can get a value. 只是说,当您使用load()方法时,属性可以获取一个值。 And if not marked as 'safe' it doesn't prevent setting a value with eg $model->dhanwantri_bill_number = 'asdf' . 而且,如果未标记为“安全”,它也不会阻止使用$model->dhanwantri_bill_number = 'asdf'设置值。 So it is not a proper solution. 因此,这不是一个适当的解决方案。

More precisely: the 'safe' attribute does not have an effect when $model->validate() (which is usually called with $model->save() ) gets called. 更准确地说:当调用$model->validate() (通常通过$model->save()调用$model->validate()时,“安全”属性无效。 It is only used when $model->load() is called. 仅在调用$model->load()时使用。 If you look into the source code of the SafeValidatior class you see that nothing happens with this validator. 如果查看SafeValidatior类的源代码,您会发现此验证器没有任何反应。 The validator doesn't do anything. 验证器不执行任何操作。 It is just a marker (you may want to compare it to eg RequiredValidator ). 它只是一个标记(您可能希望将其与RequiredValidator进行比较)。 And with load() the 'when' expression is not used. 对于load() ,不使用“ when”表达式。 So you can say 'safe' doesn't work with 'when' . 因此,您可以说“安全”与“时间”不兼容 The safe validator may get used when the rule gets evaluated but its validateAttribute() is empty so nothing happens in that point in time. 在评估规则但其validateAttribute()为空时,可以使用安全验证器,因此在该时间点没有任何反应。

Besides the whenClient in your code doesn't make sense. 除了代码中的whenClient没有意义。 What should happen here? 这里应该怎么办?

I guess there are several ways of realizing that. 我想有几种方法可以实现。 One idea would be to let the controller set the attributes not by load(), rather set them explicitely and check there if $model->dhanwantri_bill_number should be set or not. 一种想法是让控制器不通过load()设置属性,而是显式地设置它们,并检查是否应该设置$model->dhanwantri_bill_number Or you could use load() and then revert the attribute after loading according to what $model->bill_type is set. 或者您可以使用load(),然后在加载后根据设置的$model->bill_type还原属性。

Or you could implement a setter method for dhanwantri_bill_number in your model and choose there if the attribute gets set or not. 或者,您可以在模型中实现dhanwantri_bill_number的setter方法,然后选择是否设置属性。 Maybe scenario dependent. 可能取决于场景。

From Yii2 doc: 从Yii2 doc:

By default, an active attribute is considered safe and can be massively assigned. 默认情况下,活动属性被认为是安全的,可以进行大规模分配。 If an attribute should NOT be massively assigned (thus considered unsafe), please prefix the attribute with an exclamation character (eg '!rank'). 如果不应大量分配属性(因此认为不安全),请在属性前加上感叹号(例如'!rank')。

Consider to use scenarios to handle your problem. 考虑使用方案来解决您的问题。 http://www.yiiframework.com/doc-2.0/guide-structure-models.html#scenarios http://www.yiiframework.com/doc-2.0/guide-structure-models.html#scenarios

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM