1
1
import { html , customElement , css , state , when } from '@umbraco-cms/backoffice/external/lit' ;
2
2
import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element' ;
3
3
import { UMB_VALIDATION_CONTEXT , umbBindToValidation , UmbValidationContext } from '@umbraco-cms/backoffice/validation' ;
4
+ import type { UmbValidationMessage } from '@umbraco-cms/backoffice/validation' ;
4
5
5
6
@customElement ( 'umb-example-validation-context-dashboard' )
6
- export class UmbExampleValidationContextDashboard extends UmbLitElement {
7
-
7
+ export class UmbExampleValidationContextDashboardElement extends UmbLitElement {
8
8
readonly validation = new UmbValidationContext ( this ) ;
9
9
10
10
@state ( )
@@ -20,7 +20,7 @@ export class UmbExampleValidationContextDashboard extends UmbLitElement {
20
20
country = '' ;
21
21
22
22
@state ( )
23
- messages ? : any [ ]
23
+ messages ?: UmbValidationMessage [ ] ;
24
24
25
25
@state ( )
26
26
totalErrorCount = 0 ;
@@ -32,188 +32,199 @@ export class UmbExampleValidationContextDashboard extends UmbLitElement {
32
32
tab2ErrorCount = 0 ;
33
33
34
34
@state ( )
35
- tab = "1" ;
35
+ tab = '1' ;
36
36
37
37
constructor ( ) {
38
38
super ( ) ;
39
39
40
- this . consumeContext ( UMB_VALIDATION_CONTEXT , ( validationContext ) => {
41
-
42
- this . observe ( validationContext . messages . messages , ( messages ) => {
43
- this . messages = messages ;
44
- } , 'observeValidationMessages' )
40
+ this . consumeContext ( UMB_VALIDATION_CONTEXT , ( validationContext ) => {
41
+ this . observe (
42
+ validationContext . messages . messages ,
43
+ ( messages ) => {
44
+ this . messages = messages ;
45
+ } ,
46
+ 'observeValidationMessages' ,
47
+ ) ;
45
48
46
49
// Observe all errors
47
- this . validation . messages . messagesOfPathAndDescendant ( '$.form' ) . subscribe ( ( value ) => {
48
- this . totalErrorCount = [ ...new Set ( value . map ( x => x . path ) ) ] . length ;
50
+ this . validation . messages . messagesOfPathAndDescendant ( '$.form' ) . subscribe ( ( value ) => {
51
+ this . totalErrorCount = [ ...new Set ( value . map ( ( x ) => x . path ) ) ] . length ;
49
52
} ) ;
50
53
51
54
// Observe errors for tab1, note that we only use part of the full JSONPath
52
- this . validation . messages . messagesOfPathAndDescendant ( '$.form.tab1' ) . subscribe ( ( value ) => {
53
- this . tab1ErrorCount = [ ...new Set ( value . map ( x => x . path ) ) ] . length ;
55
+ this . validation . messages . messagesOfPathAndDescendant ( '$.form.tab1' ) . subscribe ( ( value ) => {
56
+ this . tab1ErrorCount = [ ...new Set ( value . map ( ( x ) => x . path ) ) ] . length ;
54
57
} ) ;
55
58
56
59
// Observe errors for tab2, note that we only use part of the full JSONPath
57
- this . validation . messages . messagesOfPathAndDescendant ( '$.form.tab2' ) . subscribe ( ( value ) => {
58
- this . tab2ErrorCount = [ ...new Set ( value . map ( x => x . path ) ) ] . length ;
60
+ this . validation . messages . messagesOfPathAndDescendant ( '$.form.tab2' ) . subscribe ( ( value ) => {
61
+ this . tab2ErrorCount = [ ...new Set ( value . map ( ( x ) => x . path ) ) ] . length ;
59
62
} ) ;
60
-
61
- } ) ;
63
+ } ) ;
62
64
}
63
65
64
- #onTabChange( e :Event ) {
66
+ #onTabChange( e : Event ) {
65
67
this . tab = ( e . target as HTMLElement ) . getAttribute ( 'data-tab' ) as string ;
66
68
}
67
69
68
70
#handleSave( ) {
69
-
70
71
// fake server validation-errors for all fields
71
- if ( this . name == '' )
72
- this . validation . messages . addMessage ( 'server' , '$.form.tab1.name' , 'Name server-error message' , '4875e113-cd0c-4c57-ac92-53d677ba31ec' ) ;
73
- if ( this . email == '' )
74
- this . validation . messages . addMessage ( 'server' , '$.form.tab1.email' , 'Email server-error message' , 'a47e287b-4ce6-4e8b-8e05-614e7cec1a2a' ) ;
75
- if ( this . city == '' )
76
- this . validation . messages . addMessage ( 'server' , '$.form.tab2.city' , 'City server-error message' , '8dfc2f15-fb9a-463b-bcec-2c5d3ba2d07d' ) ;
77
- if ( this . country == '' )
78
- this . validation . messages . addMessage ( 'server' , '$.form.tab2.country' , 'Country server-error message' , 'd98624f6-82a2-4e94-822a-776b44b01495' ) ;
72
+ if ( this . name == '' )
73
+ this . validation . messages . addMessage (
74
+ 'server' ,
75
+ '$.form.tab1.name' ,
76
+ 'Name server-error message' ,
77
+ '4875e113-cd0c-4c57-ac92-53d677ba31ec' ,
78
+ ) ;
79
+ if ( this . email == '' )
80
+ this . validation . messages . addMessage (
81
+ 'server' ,
82
+ '$.form.tab1.email' ,
83
+ 'Email server-error message' ,
84
+ 'a47e287b-4ce6-4e8b-8e05-614e7cec1a2a' ,
85
+ ) ;
86
+ if ( this . city == '' )
87
+ this . validation . messages . addMessage (
88
+ 'server' ,
89
+ '$.form.tab2.city' ,
90
+ 'City server-error message' ,
91
+ '8dfc2f15-fb9a-463b-bcec-2c5d3ba2d07d' ,
92
+ ) ;
93
+ if ( this . country == '' )
94
+ this . validation . messages . addMessage (
95
+ 'server' ,
96
+ '$.form.tab2.country' ,
97
+ 'Country server-error message' ,
98
+ 'd98624f6-82a2-4e94-822a-776b44b01495' ,
99
+ ) ;
79
100
}
80
101
81
102
override render ( ) {
82
103
return html `
83
104
<uui- box>
84
- This is a demo of how the Validation Context can be used to validate a for m with multiple steps. Start typing in the form or press Save to trigger validation .
85
- <hr/ >
105
+ This is a demo of how the Validation Context can be used to validate a for m with multiple steps. Start typing in
106
+ the form or press Save to trigger validation .
107
+ <hr / >
86
108
Total errors : ${ this . totalErrorCount }
87
- <hr/ >
109
+ <hr / >
88
110
<uui- tab- group @click = ${ this . #onTabChange} >
89
- <uui- tab ?active= ${ this . tab == '1' } data- tab= "1" >
111
+ <uui- tab ?active= ${ this . tab == '1' } data- tab= "1" >
90
112
Tab 1
91
- ${ when ( this . tab1ErrorCount , ( ) => html `
92
- <uui- badge color = "danger" > ${ this . tab1ErrorCount } </ uui- badge>
93
- ` ) }
113
+ ${ when ( this . tab1ErrorCount , ( ) => html ` <uui- badge color = "invalid" > ${ this . tab1ErrorCount } </ uui- badge> ` ) }
94
114
</ uui- tab>
95
- <uui- tab ?active= ${ this . tab == '2' } data- tab= "2" >
115
+ <uui- tab ?active= ${ this . tab == '2' } data- tab= "2" >
96
116
Tab 2
97
- ${ when ( this . tab2ErrorCount , ( ) => html `
98
- <uui- badge color = "danger" > ${ this . tab2ErrorCount } </ uui- badge>
99
- ` ) }
117
+ ${ when ( this . tab2ErrorCount , ( ) => html ` <uui- badge color = "invalid" > ${ this . tab2ErrorCount } </ uui- badge> ` ) }
100
118
</ uui- tab>
101
- </ uui- tab- group>
119
+ </ uui- tab- group>
102
120
103
- ${ when ( this . tab == '1' , ( ) => html `
104
- ${ this . #renderTab1( ) }
105
- ` ) }
106
- ${ when ( this . tab == '2' , ( ) => html `
107
- ${ this . #renderTab2( ) }
108
- ` ) }
121
+ ${ when ( this . tab == '1' , ( ) => html ` ${ this . #renderTab1( ) } ` ) }
122
+ ${ when ( this . tab == '2' , ( ) => html ` ${ this . #renderTab2( ) } ` ) }
109
123
110
124
<uui- butto n look= "primary" color = "positive" @click = ${ this . #handleSave} > Save </ uui- butto n>
111
- <hr/ >
125
+ <hr / >
112
126
<h3> Validation Context Messages </ h3>
113
- <pre> ${ JSON . stringify ( this . messages ?? [ ] , null , 3 ) } </ pre>
127
+ <pre> ${ JSON . stringify ( this . messages ?? [ ] , null , 3 ) } </ pre>
114
128
</ uui- box>
115
- `
129
+ ` ;
116
130
}
117
131
118
132
#renderTab1( ) {
119
133
return html `
120
134
<uui- for m>
121
135
<for m>
122
136
<div>
123
- <label> Name </ label>
124
- <uui- for m- validation- message>
125
- <uui- input
126
- type= "text"
127
- .value = ${ this . name }
128
- @input = ${ ( e : InputEvent ) => this . name = ( e . target as HTMLInputElement ) . value }
129
- ${ umbBindToValidation ( this , '$.form.tab1.name' , this . name ) }
130
- required> </ uui- input>
131
- </ uui- for m- validation- message>
137
+ <label> Name </ label>
138
+ <uui- for m- validation- message>
139
+ <uui- input
140
+ type= "text"
141
+ .value = ${ this . name }
142
+ @input = ${ ( e : InputEvent ) => ( this . name = ( e . target as HTMLInputElement ) . value ) }
143
+ ${ umbBindToValidation ( this , '$.form.tab1.name' , this . name ) }
144
+ required> </ uui- input>
145
+ </ uui- for m- validation- message>
132
146
</ div>
133
147
<label> E-mail </ label>
134
148
<uui- for m- validation- message>
135
149
<uui- input
136
150
type= "email"
137
151
.value = ${ this . email }
138
- @input = ${ ( e : InputEvent ) => this . email = ( e . target as HTMLInputElement ) . value }
139
- ${ umbBindToValidation ( this , '$.form.tab1.email' , this . email ) }
152
+ @input = ${ ( e : InputEvent ) => ( this . email = ( e . target as HTMLInputElement ) . value ) }
153
+ ${ umbBindToValidation ( this , '$.form.tab1.email' , this . email ) }
140
154
required> </ uui- input>
141
155
</ uui- for m- validation- message>
142
156
</ for m>
143
157
</ uui- for m>
144
- `
158
+ ` ;
145
159
}
146
160
147
161
#renderTab2( ) {
148
162
return html `
149
163
<uui- for m>
150
164
<for m>
151
165
<div>
152
- <label> City </ label>
153
- <uui- for m- validation- message>
154
- <uui- input
155
- type= "text"
156
- .value = ${ this . city }
157
- @input = ${ ( e : InputEvent ) => this . city = ( e . target as HTMLInputElement ) . value }
158
- ${ umbBindToValidation ( this , '$.form.tab2.city' , this . city ) }
159
- required> </ uui- input>
160
- </ uui- for m- validation- message>
166
+ <label> City </ label>
167
+ <uui- for m- validation- message>
168
+ <uui- input
169
+ type= "text"
170
+ .value = ${ this . city }
171
+ @input = ${ ( e : InputEvent ) => ( this . city = ( e . target as HTMLInputElement ) . value ) }
172
+ ${ umbBindToValidation ( this , '$.form.tab2.city' , this . city ) }
173
+ required> </ uui- input>
174
+ </ uui- for m- validation- message>
161
175
</ div>
162
176
<label> Country </ label>
163
177
<uui- for m- validation- message>
164
178
<uui- input
165
179
type= "text"
166
180
.value = ${ this . country }
167
- @input = ${ ( e : InputEvent ) => this . country = ( e . target as HTMLInputElement ) . value }
168
- ${ umbBindToValidation ( this , '$.form.tab2.country' , this . country ) }
181
+ @input = ${ ( e : InputEvent ) => ( this . country = ( e . target as HTMLInputElement ) . value ) }
182
+ ${ umbBindToValidation ( this , '$.form.tab2.country' , this . country ) }
169
183
required> </ uui- input>
170
184
</ uui- for m- validation- message>
171
185
</ for m>
172
186
</ uui- for m>
173
- `
187
+ ` ;
174
188
}
175
189
176
-
177
-
178
- static override styles = [ css `
179
-
180
- uui-badge {
181
- top : 0 ;
182
- right : 0 ;
183
- font-size : 10px ;
184
- min-width : 17px ;
185
- min-height : 17px ;
186
-
187
- }
188
-
189
- label {
190
- display : block;
191
- }
192
-
193
- uui-box {
194
- margin : 20px ;
195
- }
196
-
197
- uui-button {
198
- margin-top : 1rem ;
199
- }
200
-
201
- pre {
202
- text-align : left;
203
- padding : 10px ;
204
- border : 1px dotted # 6f6f6f ;
205
- background : # f2f2f2 ;
206
- font-size : 11px ;
207
- line-height : 1.3em ;
208
- }
209
-
210
- ` ]
190
+ static override styles = [
191
+ css `
192
+ uui-badge {
193
+ top : 0 ;
194
+ right : 0 ;
195
+ font-size : 10px ;
196
+ min-width : 17px ;
197
+ min-height : 17px ;
198
+ }
199
+
200
+ label {
201
+ display : block;
202
+ }
203
+
204
+ uui-box {
205
+ margin : 20px ;
206
+ }
207
+
208
+ uui-button {
209
+ margin-top : 1rem ;
210
+ }
211
+
212
+ pre {
213
+ text-align : left;
214
+ padding : 10px ;
215
+ border : 1px dotted # 6f6f6f ;
216
+ background : # f2f2f2 ;
217
+ font-size : 11px ;
218
+ line-height : 1.3em ;
219
+ }
220
+ ` ,
221
+ ] ;
211
222
}
212
223
213
- export default UmbExampleValidationContextDashboard ;
224
+ export default UmbExampleValidationContextDashboardElement ;
214
225
215
226
declare global {
216
227
interface HTMLElementTagNameMap {
217
- 'umb-example-validation-context-dashboard' : UmbExampleValidationContextDashboard ;
228
+ 'umb-example-validation-context-dashboard' : UmbExampleValidationContextDashboardElement ;
218
229
}
219
230
}
0 commit comments