Skip to content

Commit 98ff36b

Browse files
Validates and prevents save when providing a number outside of the configured range. (#17991)
* Validates and prevents save when providing a number outside of the configured range. * Refactored to make use of `UmbFormControlMixin` This flags the property layout container to be invalid (with prompt). Adds localization keys. * Corrected localization parameter order --------- Co-authored-by: leekelleher <[email protected]>
1 parent f67a900 commit 98ff36b

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/Umbraco.Web.UI.Client/src/assets/lang/en.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2126,6 +2126,9 @@ export default {
21262126
duplicateUsername: "Username '%0%' is already taken",
21272127
legacyOption: 'Legacy option',
21282128
legacyOptionDescription: 'This option is no longer supported, please select something else',
2129+
numberMinimum: "Value must be greater than or equal to '%0%'.",
2130+
numberMaximum: "Value must be less than or equal to '%0%'.",
2131+
numberMisconfigured: "Minimum value '%0%' must be less than the maximum value '%1%'.",
21292132
},
21302133
healthcheck: {
21312134
checkSuccessMessage: "Value is set to the recommended value: '%0%'.",

src/Umbraco.Web.UI.Client/src/packages/property-editors/number/property-editor-ui-number.element.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { css, customElement, html, ifDefined, property, state } from '@umbraco-cms/backoffice/external/lit';
2+
import { UmbFormControlMixin } from '@umbraco-cms/backoffice/validation';
23
import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element';
34
import { UmbPropertyValueChangeEvent } from '@umbraco-cms/backoffice/property-editor';
45
import type {
@@ -7,10 +8,10 @@ import type {
78
} from '@umbraco-cms/backoffice/property-editor';
89

910
@customElement('umb-property-editor-ui-number')
10-
export class UmbPropertyEditorUINumberElement extends UmbLitElement implements UmbPropertyEditorUiElement {
11-
@property({ type: Number })
12-
value?: number;
13-
11+
export class UmbPropertyEditorUINumberElement
12+
extends UmbFormControlMixin<number | undefined, typeof UmbLitElement, undefined>(UmbLitElement)
13+
implements UmbPropertyEditorUiElement
14+
{
1415
/**
1516
* Sets the input to readonly mode, meaning value cannot be changed but still able to read and select its content.
1617
* @type {boolean}
@@ -40,6 +41,28 @@ export class UmbPropertyEditorUINumberElement extends UmbLitElement implements U
4041
this._placeholder = config.getValueByAlias('placeholder');
4142
}
4243

44+
constructor() {
45+
super();
46+
47+
this.addValidator(
48+
'rangeUnderflow',
49+
() => this.localize.term('validation_numberMinimum', this._min),
50+
() => !!this._min && this.value! < this._min,
51+
);
52+
53+
this.addValidator(
54+
'rangeOverflow',
55+
() => this.localize.term('validation_numberMaximum', this._max),
56+
() => !!this._max && this.value! > this._max,
57+
);
58+
59+
this.addValidator(
60+
'customError',
61+
() => this.localize.term('validation_numberMisconfigured', this._min, this._max),
62+
() => !!this._min && !!this._max && this._min > this._max,
63+
);
64+
}
65+
4366
#parseInt(input: unknown): number | undefined {
4467
const num = Number(input);
4568
return Number.isNaN(num) ? undefined : num;
@@ -58,7 +81,7 @@ export class UmbPropertyEditorUINumberElement extends UmbLitElement implements U
5881
max=${ifDefined(this._max)}
5982
step=${ifDefined(this._step)}
6083
placeholder=${ifDefined(this._placeholder)}
61-
.value=${this.value ?? (this._placeholder ? undefined : 0)}
84+
value=${this.value?.toString() ?? (this._placeholder ? '' : '0')}
6285
@input=${this.#onInput}
6386
?readonly=${this.readonly}>
6487
</uui-input>

0 commit comments

Comments
 (0)