Octobercms 验证关系用户
Octobercms validation relation user
我和用户模型有如下关系
public $belongsTo = [
'user' => [
'Rainlab\User\Models\User',
'key' => 'user_id',
'order' => 'name asc'
]
];
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
字段上进行验证,但它不起作用,即使我已经 selected 用户它继续通知我需要 select用户
/**
* @var array Validation rules
*/
public $rules = [
'user' => 'required'
];
是的,I can Understand your problem
。这只会在您要添加新记录时发生。
它将完美地用于 existing record
。至于 existing record
数据是 persisted in database
所以我们可以表示一个工作记录然后我们可以在上面触发 relational validation
。
但是对于new record
没有ID意味着记录it self
是没有保存在数据库中所以有将与那个关系字段没有关系所以我们永远不知道这个字段是否附加了一些值并且验证将一直失败。
所以无论你添加多少记录,它都会显示 ERROR each time
"please select user"。
October CMS 使用differ binding
你可以看到你可以在不保存当前记录的情况下添加用户。因为该数据存储在中间 table 所以在创建记录后 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
.
首先 从 rules
中删除字段 user
/**
* @var array Validation rules
*/
public $rules = [
'user' => 'required' <-- Remove this
];
现在我们会manual validation
Add this code
to your 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
添加不同的验证。
现在验证将正常工作。
如果您仍然发现问题,请发表评论。
我和用户模型有如下关系
public $belongsTo = [
'user' => [
'Rainlab\User\Models\User',
'key' => 'user_id',
'order' => 'name asc'
]
];
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
字段上进行验证,但它不起作用,即使我已经 selected 用户它继续通知我需要 select用户
/**
* @var array Validation rules
*/
public $rules = [
'user' => 'required'
];
是的,I can Understand your problem
。这只会在您要添加新记录时发生。
它将完美地用于 existing record
。至于 existing record
数据是 persisted in database
所以我们可以表示一个工作记录然后我们可以在上面触发 relational validation
。
但是对于new record
没有ID意味着记录it self
是没有保存在数据库中所以有将与那个关系字段没有关系所以我们永远不知道这个字段是否附加了一些值并且验证将一直失败。
所以无论你添加多少记录,它都会显示 ERROR each time
"please select user"。
October CMS 使用differ binding
你可以看到你可以在不保存当前记录的情况下添加用户。因为该数据存储在中间 table 所以在创建记录后 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
.
首先 从 rules
user
/**
* @var array Validation rules
*/
public $rules = [
'user' => 'required' <-- Remove this
];
现在我们会manual validation
Add this
code
to yourmodel
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
添加不同的验证。
现在验证将正常工作。
如果您仍然发现问题,请发表评论。