Octobercms 后端列表选择的最大限制
Octobercm Backend List maximum limit of selections
我正在开发一个图书馆系统Octobercms,我需要限制每次借书的最大数量,例如:2。这样当你达到selections的数量时,不要请允许我 select 借出更多书籍。
见图:
Backend List
请帮帮我!
为了让它工作,我们需要采用 different approach
,instead adding error when we are selecting
我们可以抛出错误 when saving
,(因为在那里添加 java-script 也有点难它的客户端如此不安全)。
只需将此 code
添加到您的 controller
和 change fields according to need
。
public function onRelationManageAdd() {
$checked = \Input::get('checked');
$field = \Input::get('_relation_field');
$maximumAllowed = 2;
// i have used comment $field you need to replace it with your field
// this will be your hasMany relational field name
// also added condition to check selected id is more then 2
if($field == 'comments' && is_array($checked) && count($checked) > $maximumAllowed) {
// if selected id is more then 2 we add flash error and return blank array
\Flash::error('You Can Select Only 2 !');
return [];
}
// ADDITIONAL CHECK if you need more check you can add it here and return error
if($field == 'comments' && isset($this->params[0])) {
// currently editing record id.
$currentEditRecordId = $this->params[0];
$record = \October\Test\Models\Post::find($currentEditRecordId);
if($record && $record->comments->count() >= $maximumAllowed) {
\Flash::error('You Can Select Only 2 !');
return [];
}
}
// if everything is good then handle request by relation manger
//and return response
return $this->asExtension('RelationController')->onRelationManageAdd();
}
我添加了 ADDITIONAL CHECK
因为这将允许用户 select 2 records per one time
,但用户可以 select 2 records multiple time
但我们不允许他们 ;)
你可以添加custom validations
你喜欢的。
更新
问题 : select 第一次记录 1 然后 select 另一个记录 - 解决方案
public function onRelationManageAdd() {
$checked = \Input::get('checked');
$field = \Input::get('_relation_field');
$maximumAllowed = 2;
$count = 0;
// i have used comment $field you need to replace it with your field
// this will be your hasMany relational field name
// also added condition to check selected id is more then 2
if($field == 'comments' && is_array($checked)) {
//$count += count($checked);
$count = $count + count($checked);
}
// ADDITIONAL CHECK if you need more check you can add it here and return error
if($field == 'comments' && isset($this->params[0])) {
// currently editing record id.
$currentEditRecordId = $this->params[0];
$record = \October\Test\Models\Post::find($currentEditRecordId);
if($record) {
$count = $count + $record->comments->count();
}
}
if($count > $maximumAllowed) {
// if selected id is more then 2 we add flash error and return blank array
\Flash::error('You Can Select Only 2 !');
return [];
}
// if everything is good then handle request by relation manger
//and return response
return $this->asExtension('RelationController')->onRelationManageAdd();
}
信息:你可以添加这个relational field in update context
当用户adding new record
那个时候we don't have information for current record
作为当前记录is not saved yet
所以 relation count
将 在 第二次检查 和我们的 validation will fail
.
中不可用
要避免此问题,您可以为该字段添加更新上下文,该字段将仅在 update
.
中可用
banner:
label: Banner
oc.commentPosition: ''
mode: file
span: auto
type: mediafinder
context: update <------ add this option
现在这个字段只会在用户保存记录时显示。我们现在可以很好地进行验证了。
如果您有任何错误,请发表评论。
我正在开发一个图书馆系统Octobercms,我需要限制每次借书的最大数量,例如:2。这样当你达到selections的数量时,不要请允许我 select 借出更多书籍。
见图: Backend List
请帮帮我!
为了让它工作,我们需要采用 different approach
,instead adding error when we are selecting
我们可以抛出错误 when saving
,(因为在那里添加 java-script 也有点难它的客户端如此不安全)。
只需将此 code
添加到您的 controller
和 change fields according to need
。
public function onRelationManageAdd() {
$checked = \Input::get('checked');
$field = \Input::get('_relation_field');
$maximumAllowed = 2;
// i have used comment $field you need to replace it with your field
// this will be your hasMany relational field name
// also added condition to check selected id is more then 2
if($field == 'comments' && is_array($checked) && count($checked) > $maximumAllowed) {
// if selected id is more then 2 we add flash error and return blank array
\Flash::error('You Can Select Only 2 !');
return [];
}
// ADDITIONAL CHECK if you need more check you can add it here and return error
if($field == 'comments' && isset($this->params[0])) {
// currently editing record id.
$currentEditRecordId = $this->params[0];
$record = \October\Test\Models\Post::find($currentEditRecordId);
if($record && $record->comments->count() >= $maximumAllowed) {
\Flash::error('You Can Select Only 2 !');
return [];
}
}
// if everything is good then handle request by relation manger
//and return response
return $this->asExtension('RelationController')->onRelationManageAdd();
}
我添加了 ADDITIONAL CHECK
因为这将允许用户 select 2 records per one time
,但用户可以 select 2 records multiple time
但我们不允许他们 ;)
你可以添加custom validations
你喜欢的。
更新
问题 : select 第一次记录 1 然后 select 另一个记录 - 解决方案
public function onRelationManageAdd() {
$checked = \Input::get('checked');
$field = \Input::get('_relation_field');
$maximumAllowed = 2;
$count = 0;
// i have used comment $field you need to replace it with your field
// this will be your hasMany relational field name
// also added condition to check selected id is more then 2
if($field == 'comments' && is_array($checked)) {
//$count += count($checked);
$count = $count + count($checked);
}
// ADDITIONAL CHECK if you need more check you can add it here and return error
if($field == 'comments' && isset($this->params[0])) {
// currently editing record id.
$currentEditRecordId = $this->params[0];
$record = \October\Test\Models\Post::find($currentEditRecordId);
if($record) {
$count = $count + $record->comments->count();
}
}
if($count > $maximumAllowed) {
// if selected id is more then 2 we add flash error and return blank array
\Flash::error('You Can Select Only 2 !');
return [];
}
// if everything is good then handle request by relation manger
//and return response
return $this->asExtension('RelationController')->onRelationManageAdd();
}
信息:你可以添加这个relational field in update context
当用户adding new record
那个时候we don't have information for current record
作为当前记录is not saved yet
所以 relation count
将 在 第二次检查 和我们的 validation will fail
.
要避免此问题,您可以为该字段添加更新上下文,该字段将仅在 update
.
banner:
label: Banner
oc.commentPosition: ''
mode: file
span: auto
type: mediafinder
context: update <------ add this option
现在这个字段只会在用户保存记录时显示。我们现在可以很好地进行验证了。
如果您有任何错误,请发表评论。