link multiple maxCount

This commit is contained in:
Yuri Kuznetsov
2024-04-20 20:50:03 +03:00
parent b604501eba
commit a081237b2e
5 changed files with 117 additions and 19 deletions
+33 -1
View File
@@ -55,6 +55,7 @@ class AttachmentMultipleFieldView extends BaseFieldView {
* @property {string[]} [sourceList] A source list.
* @property {string[]} [accept] Formats to accept.
* @property {number} [maxFileSize] A max file size (in Mb).
* @property {number} [maxCount] A max number of items.
*/
/**
@@ -81,7 +82,11 @@ class AttachmentMultipleFieldView extends BaseFieldView {
foreignScope
showPreviews = true
accept = null
validations = ['ready', 'required']
validations = [
'ready',
'required',
'maxCount',
]
searchTypeList = ['isNotEmpty', 'isEmpty']
events = {
@@ -932,6 +937,33 @@ class AttachmentMultipleFieldView extends BaseFieldView {
}
}
// noinspection JSUnusedGlobalSymbols
validateMaxCount() {
const maxCount = this.params.maxCount;
if (!maxCount) {
return false;
}
const idList = this.model.get(this.idsName) || [];
if (idList.length === 0) {
return false;
}
if (idList.length <= maxCount) {
return false;
}
const msg = this.translate('fieldExceedsMaxCount', 'messages')
.replace('{field}', this.getLabelText())
.replace('{maxCount}', maxCount.toString());
this.showValidationMessage(msg, 'label');
return true;
}
fetch() {
const data = {};
+33
View File
@@ -54,6 +54,7 @@ class LinkMultipleFieldView extends BaseFieldView {
* @property {boolean} [autocompleteOnEmpty] Autocomplete on empty input.
* @property {boolean} [sortable] Sortable.
* @property {boolean} [createButton] Show 'Create' button.
* @property {number} [maxCount] A max number of items.
*/
/**
@@ -73,6 +74,11 @@ class LinkMultipleFieldView extends BaseFieldView {
editTemplate = 'fields/link-multiple/edit'
searchTemplate = 'fields/link-multiple/search'
validations = [
'required',
'maxCount',
]
/**
* A name-hash attribute name.
*
@@ -831,6 +837,33 @@ class LinkMultipleFieldView extends BaseFieldView {
return false;
}
// noinspection JSUnusedGlobalSymbols
validateMaxCount() {
const maxCount = this.params.maxCount;
if (!maxCount) {
return false;
}
const idList = this.model.get(this.idsName) || [];
if (idList.length === 0) {
return false;
}
if (idList.length <= maxCount) {
return false;
}
const msg = this.translate('fieldExceedsMaxCount', 'messages')
.replace('{field}', this.getLabelText())
.replace('{maxCount}', maxCount.toString());
this.showValidationMessage(msg);
return true;
}
/** @inheritDoc */
fetch() {
const data = {};