Skip to content

Commit 600c968

Browse files
nielslyngsoegitbook-bot
authored andcommitted
GITBOOK-25: Property Editor Validation
1 parent 86d874a commit 600c968

File tree

4 files changed

+89
-1
lines changed

4 files changed

+89
-1
lines changed

16/umbraco-cms/SUMMARY.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,15 @@
203203
* [UI Sorting](customizing/foundation/sorting.md)
204204
* [Routes](customizing/foundation/routes.md)
205205
* [Backoffice Localization](customizing/foundation/localization.md)
206+
* [Integrate Validation](customizing/foundation/integrate-validation.md)
206207
* [Contexts](customizing/foundation/contexts/README.md)
207208
* [Property Dataset Context](customizing/foundation/contexts/property-dataset-context.md)
208209
* [Sections & Trees](customizing/section-trees.md)
209210
* [Property Level UI Permissions](customizing/property-level-ui-permissions.md)
210211
* [Icons](customizing/foundation/icons.md)
211212
* [Searchable Trees (ISearchableTree)](customizing/searchable-trees.md)
212213
* [Property Editors](customizing/property-editors/README.md)
214+
* [Property Editor Validation](customizing/property-editors/property-editor-validation.md)
213215
* [Property Editors Composition](customizing/property-editors/composition/README.md)
214216
* [Property Editor Schema](customizing/property-editors/composition/property-editor-schema.md)
215217
* [Property Editor UI](customizing/property-editors/composition/property-editor-ui.md)
@@ -219,7 +221,6 @@
219221
* [Tracking References](customizing/property-editors/tracking.md)
220222
* [Content Picker Value Converter Example](customizing/property-editors/full-examples-value-converters.md)
221223
* [Property Dataset](customizing/property-editors/property-dataset.md)
222-
* [Integrate Validation](customizing/property-editors/integrate-validation.md)
223224
* [Workspaces](customizing/workspaces.md)
224225
* [Umbraco Package](customizing/umbraco-package.md)
225226
* [UI Library](customizing/ui-library.md)
File renamed without changes.

16/umbraco-cms/customizing/property-editors/property-dataset.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
---
2+
description: >-
3+
Looking to implement one or more Property Editors in your own scenario? The
4+
Property Dataset is necessary for a Property Editor to work, so make sure to
5+
have that integrated first.
6+
---
7+
18
# Property Dataset
29

310
A Property Dataset is a Context API that holds the data for a set of properties.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
---
2+
description: >-
3+
Looking to add Validation rules for your own Property Editor? This article
4+
describes how to append validation rules to your Property Editor.
5+
---
6+
7+
# Property Editor Validation
8+
9+
Adding Validation rules to a Property Editor is the same as adding Validation Rules to any Web Component. But it does require that your Web Component be a Form Control.
10+
11+
To achieve this, you can use a Mixin called `UmbFormControlMixin` . The following example shows how this could look:
12+
13+
```typescript
14+
import { customElement } from '@umbraco-cms/backoffice/external/lit';
15+
import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element';
16+
import { UmbFormControlMixin } from '@umbraco-cms/backoffice/validation';
17+
18+
@customElement('my-property-editor')
19+
export class MyPropertyEditorElement
20+
extends UmbFormControlMixin<string | undefined, typeof UmbLitElement, undefined>(UmbLitElement)
21+
implements UmbPropertyEditorUiElement {
22+
23+
/**
24+
Notice 'value'-property is already defined via the FormControlMixin, based on the first generic type given to it
25+
*/
26+
27+
...
28+
29+
}
30+
31+
export default MyPropertyEditorElement;
32+
33+
declare global {
34+
interface HTMLElementTagNameMap {
35+
'my-property-editor': MyPropertyEditorElement;
36+
}
37+
}
38+
39+
```
40+
41+
Once your Property Editor is a Form Control, you can append validation rules to it.
42+
43+
```typescript
44+
...
45+
46+
constructor() {
47+
super();
48+
49+
this.addValidator(
50+
'customError',
51+
() => 'This field must contain a "Unicorn"',
52+
() => !this.value.includes("unicorn"),
53+
);
54+
}
55+
56+
...
57+
```
58+
59+
The first argument declares what type of validation error you are evaluating. You can see the various types that browsers support on the [Mozilla Development Docs](https://developer.mozilla.org/en-US/docs/Web/API/ValidityState#instance_properties).
60+
61+
The second argument is a method that returns the message; this message will be displayed if the Validator Check returns `true`.
62+
63+
The third argument is the Validator check, which is executed every time there is a change in the properties of this Web Component. If it returns `true` , the component will turn invalid, and the messages of argument two will be used as feedback to the user.
64+
65+
{% hint style="info" %}
66+
If you're looking to make server requests as part of your check, then it is recommended to avoid calling the server more often than needed. Do only ask the server if there is a value, and only ask the server again if the value is different from last time.
67+
{% endhint %}
68+
69+
Notice that `value` is already defined in the FormControlMixin, and ideally, you do not overwrite it. If so, then please make sure to still set the value property of the Mixin. As that will trigger a validation update.
70+
71+
```typescript
72+
@property({ type: String })
73+
public override set value(value: string) {
74+
// [ What you needed to do... ]
75+
super.value = value;
76+
}
77+
public override get value() {
78+
return super.value;
79+
}
80+
```

0 commit comments

Comments
 (0)