|
| 1 | +--- |
| 2 | +description: Learn how to bind and use the validation system when working with Form Controls and Umbraco CMS backoffice. |
| 3 | +--- |
| 4 | + |
| 5 | +# Integrate Validation |
| 6 | + |
| 7 | +The Validation System provides abilities to validate different Form Controls. Such can be native or custom, like a Property Editor. |
| 8 | + |
| 9 | +It also allows for binding server validation to the Form Controls making the validation experience as synergetic as possible. |
| 10 | + |
| 11 | +## Validation Context |
| 12 | + |
| 13 | +Validation Context, the hub of the Validation, is the core of this system. Everything that holds opinions about the Validation, is a Validator and is connected to the Validation Context. |
| 14 | + |
| 15 | +You can ask the Validation Context to validate. This will evaluate all validators, and once all validator instances have been validated successfully, the Validation Context will be valid. |
| 16 | + |
| 17 | +## Validators |
| 18 | + |
| 19 | +We provide a few built-in Validators that handle most cases. |
| 20 | + |
| 21 | +### Form Control Validator |
| 22 | + |
| 23 | +This Validator binds a Form Control Element with the Validation Context. When the Form Control becomes Invalid, its Validation Message is appended to the Validation Context. |
| 24 | + |
| 25 | +Notice this one also comes as a Lit Directive called `umbBindToValidation`. This enables you to integrate an element with one line of code within a Lit Render method. See the following example for a demonstration: |
| 26 | + |
| 27 | +{% code %} |
| 28 | +```typescript |
| 29 | + |
| 30 | +#validation = new UmbValidationContext(this); |
| 31 | + |
| 32 | +#validate = () => { |
| 33 | + this.#validation.validate().then(() => { |
| 34 | + console.log('Valid'); |
| 35 | + }, () => { |
| 36 | + console.log('Invalid'); |
| 37 | + }); |
| 38 | +} |
| 39 | + |
| 40 | +render() { |
| 41 | + return html` |
| 42 | + <uui-form-validation-message> |
| 43 | + <uui-input |
| 44 | + ... |
| 45 | + required |
| 46 | + ${umbBindToValidation(this)} |
| 47 | + ></uui-input> |
| 48 | + </uui-form-validation-message> |
| 49 | + <uui-button @click=${this.#validate}>Validate</uui-button> |
| 50 | + `; |
| 51 | +} |
| 52 | +``` |
| 53 | +{% endcode %} |
| 54 | + |
| 55 | +## Integrate Umb-Property Elements |
| 56 | + |
| 57 | +The `umb-property` element automatically binds to its nearest validation context. |
| 58 | + |
| 59 | +This is demonstrated in the example below: |
| 60 | + |
| 61 | +{% code %} |
| 62 | +```typescript |
| 63 | + |
| 64 | +#validation = new UmbValidationContext(this); |
| 65 | + |
| 66 | +#validate = () => { |
| 67 | + this.#validation.validate().then(() => { |
| 68 | + console.log('Valid'); |
| 69 | + }, () => { |
| 70 | + console.log('Invalid'); |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | +render() { |
| 75 | + return html` |
| 76 | + <umb-property |
| 77 | + alias="myProperty" |
| 78 | + property-editor-ui-alias="Umb.PropertyEditorUi.TextBox" |
| 79 | + ... |
| 80 | + > |
| 81 | + </umb-property> |
| 82 | + <uui-button @click=${this.#validate}>Validate</uui-button> |
| 83 | + `; |
| 84 | +} |
| 85 | +``` |
| 86 | +{% endcode %} |
| 87 | + |
| 88 | +## Server Validation and more |
| 89 | + |
| 90 | +This documentation is not available at the moment. For the moment you can find more information in the [Backoffice reposiroty](https://github.com/umbraco/Umbraco.CMS.Backoffice/tree/main/src/packages/core/validation). |
0 commit comments