Octobercms验证关系用户 [英] Octobercms validation relation user
问题描述
我与用户模型具有以下关系
I have the following relation with the user model
public $belongsTo = [
'user' => [
'Rainlab\User\Models\User',
'key' => 'user_id',
'order' => 'name asc'
]
];
config_relation.yaml
config_relation.yaml
user:
label: Usuários
view:
form: $/rainlab/user/models/user/fields.yaml
toolbarButtons: create|link
manage:
showSearch: true
showCheckBoxes: true
recordsPerPage: 10
list: $/rainlab/user/models/user/columns.yaml
form: $/rainlab/user/models/user/fields.yaml
我正在对user
字段进行验证,但是即使我已经选择了用户,它也无法正常工作,它会继续提示我需要选择用户
I am doing the validation on the user
field, but it is not working, even though I have already selected the user it continues informing that I need to select the user
/**
* @var array Validation rules
*/
public $rules = [
'user' => 'required'
];
推荐答案
是的,I can Understand your problem
.仅当您要添加新记录时才会发生这种情况.
Yes, I can Understand your problem
. this will occur only when you are going to add new record.
它对于existing record
来说将是完美的.因为existing record
数据是persisted in database
,所以我们可以表示一个工作记录,然后可以在其上触发relational validation
.
It will work perfectly for existing record
. as for existing record
data is persisted in database
so we can represent a working record and then we can fire relational validation
on it.
但是对于new record
,没有 ID ,这意味着记录it self
没有未保存在数据库中,因此与该关系字段没有任何关系,因此我们永远不会知道此字段是否附有某些值,并且验证将一直失败.
but for new record
there is no ID means record it self
is not saved in database so there will be no relation with that relational field so we never know this field has some value attached to it or not and validation will gonna fail all the time.
因此,无论您添加多少记录,都会在ERROR each time
中显示请选择用户".
so no matter how much record you add, it will show ERROR each time
that "please select user".
10月CMS使用differ binding
,您可以看到可以添加用户而不保存当前记录.因为该数据存储在中间表中,所以在创建记录后relation data will be transferred to created record
,因为现在它具有it's own ID and persisted in database
.
October CMS use differ binding
you can see you can add users without saving current record. as that data is stored in intermediate table so after record is created that relation data will be transferred to created record
because now it has it's own ID and persisted in database
.
因此对于解决方案,您需要使用
differed binding scope
在该模型内手动添加验证.
so for solution you need add validation manually inside that model, with
differed binding scope
.
首先从rules
/**
* @var array Validation rules
*/
public $rules = [
'user' => 'required' <-- Remove this
];
现在,我们将manual validation
将此
code
添加到您的model
public function beforeValidate() {
// we need to check record is created or not
if($this->id == NULL) {
// CREATE CASE
// we need to use differ binding scope as this record is not saved yet.
if($this->user()->withDeferred(post('_session_key'))->count() == 0) {
throw new \ValidationException(['user' => 'We need User !']);
}
}
else {
// UPDATE CASE
// now record is created so we dont need differ binding
if($this->user()->count() == 0) {
throw new \ValidationException(['user' => 'We need User !']);
}
}
}
现在,验证可以用于both case
,并且您可以为different cases
添加其他验证.
Now Validation can work for both case
and you can add different validation for different cases
.
现在验证将正常工作.
如果您仍然发现问题,请发表评论.
if you still find issue please comment.
这篇关于Octobercms验证关系用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!