挺着急的,在线等 [ 2.0 版本 ]
场景的验证规则
['state', 'required', 'when' => function ($model) {
return $model->country == 'USA';
}, 'whenClient' => "function (attribute, value) {
return $('#country').value == 'USA';
}"],
这个能写自定函数在里面吗。还是只能判断 真 假
'when'=>function($model){
}
还有
yii\base\Model::addError(‘name’,‘错误内容’) ;
怎么打印不出类似 name:['错误内容']
直接就一个空白页面给我
最佳答案
其他 1 个回答
-
johnny1991 回答于 2018-11-06 23:48 举报
yii\validators\Validate的源码,应该是可以的
public function validateAttributes($model, $attributes = null) { if (is_array($attributes)) { $newAttributes = []; $attributeNames = $this->getAttributeNames(); foreach ($attributes as $attribute) { if (in_array($attribute, $attributeNames, true)) { $newAttributes[] = $attribute; } } $attributes = $newAttributes; } else { $attributes = $this->getAttributeNames(); } foreach ($attributes as $attribute) { $skip = $this->skipOnError && $model->hasErrors($attribute) || $this->skipOnEmpty && $this->isEmpty($model->$attribute); if (!$skip) { if ($this->when === null || call_user_func($this->when, $model, $attribute)) { $this->validateAttribute($model, $attribute); } } } }
共 4 条回复
开门_查水表
注册时间:2018-09-28
最后登录:2018-11-14
在线时长:6小时2分
最后登录:2018-11-14
在线时长:6小时2分
- 粉丝0
- 金钱115
- 威望0
- 积分175