[英]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
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 我正在对user
字段进行验证,但是即使我已经选择了该user
,它也无法正常工作,它会继续通知我需要选择该用户
/**
* @var array Validation rules
*/
public $rules = [
'user' => 'required'
];
Yes, I can Understand your problem
. 是的, I can Understand your problem
。 this will occur only when you are going to add new record. 仅当您要添加新记录时才会发生这种情况。
It will work perfectly for existing record
. 它可以完美地用于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. 因为existing record
数据将保留persisted in database
因此我们可以表示一个工作记录,然后可以对其进行relational validation
。
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. 但是对于new record
,没有ID意味着记录it self
不会保存在数据库中,因此不会与该关系字段建立任何关系,因此我们永远不知道此字段是否具有附加值,并且验证将始终失败。
so no matter how much record you add, it will show ERROR each time
that "please select user". 因此,无论您添加多少记录, ERROR each time
“请选择用户”都会显示ERROR each time
。
October CMS use differ binding
you can see you can add users without saving current record. 十月CMS使用differ binding
您可以看到可以添加用户而不保存当前记录。 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
. 因为该数据存储在中间表中,所以在创建记录后, relation data will be transferred to created record
因为现在它具有it's own ID and persisted in database
。
so for solution you need add validation manually inside that model, with
differed binding scope
. 因此对于解决方案,您需要在differed binding scope
模型内手动添加验证。
First remove field user
from rules
首先从rules
删除现场user
/**
* @var array Validation rules
*/
public $rules = [
'user' => 'required' <-- Remove this
];
Now we will do manual validation
现在我们将进行manual validation
Add this
code
to yourmodel
将此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 !']);
}
}
}
Now Validation can work for both case
and you can add different validation for different cases
. 现在,验证可以同时适用于both case
,您可以为different cases
添加不同的验证。
Now Validation will work properly. 现在验证将正常工作。
if you still find issue please comment. 如果您仍然发现问题,请发表评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.