diff --git a/client/src/views/fields/address.js b/client/src/views/fields/address.js index 44ebf580fb..3f9b13d145 100644 --- a/client/src/views/fields/address.js +++ b/client/src/views/fields/address.js @@ -58,7 +58,10 @@ class AddressFieldView extends BaseFieldView { stateField countryField - /** @inheritDoc */ + /** + * @inheritDoc + * @type {Array<(function (): boolean)|string>} + */ validations = [ 'required', 'pattern', diff --git a/client/src/views/fields/array.js b/client/src/views/fields/array.js index ff19338bbc..7bc8c87068 100644 --- a/client/src/views/fields/array.js +++ b/client/src/views/fields/array.js @@ -87,7 +87,13 @@ class ArrayFieldView extends BaseFieldView { searchTypeList = ['anyOf', 'noneOf', 'allOf', 'isEmpty', 'isNotEmpty'] maxItemLength = null + + /** + * @inheritDoc + * @type {Array<(function (): boolean)|string>} + */ validations = ['required', 'maxCount'] + MAX_ITEM_LENGTH = 100 /** diff --git a/client/src/views/fields/attachment-multiple.js b/client/src/views/fields/attachment-multiple.js index 388ec921a9..13a6174378 100644 --- a/client/src/views/fields/attachment-multiple.js +++ b/client/src/views/fields/attachment-multiple.js @@ -82,11 +82,17 @@ class AttachmentMultipleFieldView extends BaseFieldView { foreignScope showPreviews = true accept = null + + /** + * @inheritDoc + * @type {Array<(function (): boolean)|string>} + */ validations = [ 'ready', 'required', 'maxCount', ] + searchTypeList = ['isNotEmpty', 'isEmpty'] events = { diff --git a/client/src/views/fields/base.js b/client/src/views/fields/base.js index 498c7100d2..b937694847 100644 --- a/client/src/views/fields/base.js +++ b/client/src/views/fields/base.js @@ -137,9 +137,12 @@ class BaseFieldView extends View { editTemplateContent /** - * A validation list. There should be a `validate{Name}` method for each item. + * A validation list. A function returning true if non-valid, or a name. + * For the latter, there should be a `validate{Name}` method in the class. * - * @type {string[]} + * Functions are supported as of v8.4. + * + * @type {Array<(function (): boolean)|string>} */ validations = ['required'] @@ -1542,10 +1545,18 @@ class BaseFieldView extends View { validate() { this.lastValidationMessage = null; - for (const i in this.validations) { - const method = 'validate' + Espo.Utils.upperCaseFirst(this.validations[i]); + for (const item of this.validations) { + let notValid = false; - if (this[method].call(this)) { + if (typeof item === 'function') { + notValid = item(); + } else { + const method = 'validate' + Espo.Utils.upperCaseFirst(item); + + notValid = this[method].call(this); + } + + if (notValid) { this.trigger('invalid'); return true; diff --git a/client/src/views/fields/currency.js b/client/src/views/fields/currency.js index 9d1061d37a..feaf88f96d 100644 --- a/client/src/views/fields/currency.js +++ b/client/src/views/fields/currency.js @@ -90,6 +90,10 @@ class CurrencyFieldView extends FloatFieldView { maxDecimalPlaces = 3 + /** + * @inheritDoc + * @type {Array<(function (): boolean)|string>} + */ validations = [ 'required', 'number', diff --git a/client/src/views/fields/date.js b/client/src/views/fields/date.js index 4cc8d24c9a..ee6bbc2d21 100644 --- a/client/src/views/fields/date.js +++ b/client/src/views/fields/date.js @@ -71,6 +71,10 @@ class DateFieldView extends BaseFieldView { editTemplate = 'fields/date/edit' searchTemplate = 'fields/date/search' + /** + * @inheritDoc + * @type {Array<(function (): boolean)|string>} + */ validations = [ 'required', 'date', diff --git a/client/src/views/fields/datetime.js b/client/src/views/fields/datetime.js index 18d1124292..e663e32926 100644 --- a/client/src/views/fields/datetime.js +++ b/client/src/views/fields/datetime.js @@ -69,7 +69,16 @@ class DatetimeFieldView extends DateFieldView { editTemplate = 'fields/datetime/edit' - validations = ['required', 'datetime', 'after', 'before'] + /** + * @inheritDoc + * @type {Array<(function (): boolean)|string>} + */ + validations = [ + 'required', + 'datetime', + 'after', + 'before', + ] searchTypeList = [ 'lastSevenDays', diff --git a/client/src/views/fields/float.js b/client/src/views/fields/float.js index ae789582ae..d7e8f62ad2 100644 --- a/client/src/views/fields/float.js +++ b/client/src/views/fields/float.js @@ -70,9 +70,14 @@ class FloatFieldView extends IntFieldView { editTemplate = 'fields/float/edit' decimalMark = '.' - validations = ['required', 'float', 'range'] decimalPlacesRawValue = 10 + /** + * @inheritDoc + * @type {Array<(function (): boolean)|string>} + */ + validations = ['required', 'float', 'range'] + /** @inheritDoc */ setup() { super.setup(); diff --git a/client/src/views/fields/int.js b/client/src/views/fields/int.js index c8e6132289..57dab59b4f 100644 --- a/client/src/views/fields/int.js +++ b/client/src/views/fields/int.js @@ -71,6 +71,11 @@ class IntFieldView extends BaseFieldView { detailTemplate = 'fields/int/detail' editTemplate = 'fields/int/edit' searchTemplate = 'fields/int/search' + + /** + * @inheritDoc + * @type {Array<(function (): boolean)|string>} + */ validations = ['required', 'int', 'range'] thousandSeparator = ',' diff --git a/client/src/views/fields/link-multiple.js b/client/src/views/fields/link-multiple.js index 6cbc09024f..2b0d1117ca 100644 --- a/client/src/views/fields/link-multiple.js +++ b/client/src/views/fields/link-multiple.js @@ -74,6 +74,10 @@ class LinkMultipleFieldView extends BaseFieldView { editTemplate = 'fields/link-multiple/edit' searchTemplate = 'fields/link-multiple/search' + /** + * @inheritDoc + * @type {Array<(function (): boolean)|string>} + */ validations = [ 'required', 'maxCount', diff --git a/client/src/views/fields/url.js b/client/src/views/fields/url.js index fb3dbdcb4a..31d1ee1476 100644 --- a/client/src/views/fields/url.js +++ b/client/src/views/fields/url.js @@ -70,6 +70,10 @@ class UrlFieldView extends VarcharFieldView { detailTemplate = 'fields/url/detail' defaultProtocol = 'https:' + /** + * @inheritDoc + * @type {Array<(function (): boolean)|string>} + */ validations = [ 'required', 'valid', diff --git a/client/src/views/fields/varchar.js b/client/src/views/fields/varchar.js index 9ed6da54e2..3d7b369d3e 100644 --- a/client/src/views/fields/varchar.js +++ b/client/src/views/fields/varchar.js @@ -88,7 +88,10 @@ class VarcharFieldView extends BaseFieldView { 'isNotEmpty', ] - /** @inheritDoc */ + /** + * @inheritDoc + * @type {Array<(function (): boolean)|string>} + */ validations = [ 'required', 'pattern',