@@ -113,7 +113,7 @@ class Pattern extends BasePattern {
113113 }
114114
115115 // In any case, clear the custom validity first.
116- this . set_validity ( { input : input , msg : "" } ) ;
116+ this . set_error ( { input : input , msg : "" , skip_event : true } ) ;
117117 const validity_state = input . validity ;
118118
119119 if ( event ?. submitter ?. hasAttribute ( "formnovalidate" ) ) {
@@ -148,7 +148,7 @@ class Pattern extends BasePattern {
148148 const message =
149149 input_options . message . equality ||
150150 `The value is not equal to %{attribute}` ;
151- this . set_validity ( {
151+ this . set_error ( {
152152 input : input ,
153153 msg : message ,
154154 attribute : input_options . equality ,
@@ -216,7 +216,7 @@ class Pattern extends BasePattern {
216216 ) ;
217217 msg_attr = msg_attr || not_after_el . name ;
218218 }
219- this . set_validity ( {
219+ this . set_error ( {
220220 input : input ,
221221 msg : msg || msg_default_not_after ,
222222 attribute : msg_attr . trim ( ) ,
@@ -237,7 +237,7 @@ class Pattern extends BasePattern {
237237 ) ;
238238 msg_attr = msg_attr || not_before_el . name ;
239239 }
240- this . set_validity ( {
240+ this . set_error ( {
241241 input : input ,
242242 msg : msg || msg_default_not_before ,
243243 attribute : msg_attr . trim ( ) ,
@@ -266,15 +266,15 @@ class Pattern extends BasePattern {
266266 // Default error cases with custom messages.
267267
268268 if ( validity_state . valueMissing && input_options . message . required ) {
269- this . set_validity ( { input : input , msg : input_options . message . required } ) ;
269+ this . set_error ( { input : input , msg : input_options . message . required } ) ;
270270 } else if ( validity_state . rangeUnderflow && input_options . message . min ) {
271- this . set_validity ( {
271+ this . set_error ( {
272272 input : input ,
273273 msg : input_options . message . min ,
274274 min : input . getAttribute ( "min" ) ,
275275 } ) ;
276276 } else if ( validity_state . rangeOverflow && input_options . message . max ) {
277- this . set_validity ( {
277+ this . set_error ( {
278278 input : input ,
279279 msg : input_options . message . max ,
280280 max : input . getAttribute ( "max" ) ,
@@ -284,38 +284,43 @@ class Pattern extends BasePattern {
284284 input . type === "number" &&
285285 input_options . message . number
286286 ) {
287- this . set_validity ( { input : input , msg : input_options . message . number } ) ;
287+ this . set_error ( { input : input , msg : input_options . message . number } ) ;
288288 } else if (
289289 validity_state . typeMismatch &&
290290 input . type === "email" &&
291291 input_options . message . email
292292 ) {
293- this . set_validity ( { input : input , msg : input_options . message . email } ) ;
293+ this . set_error ( { input : input , msg : input_options . message . email } ) ;
294294 } else if (
295295 validity_state . rangeUnderflow &&
296296 input . type === "date" &&
297297 input_options . message . date
298298 ) {
299- this . set_validity ( { input : input , msg : input_options . message . date } ) ;
299+ this . set_error ( { input : input , msg : input_options . message . date } ) ;
300300 } else if (
301301 validity_state . rangeOverflow &&
302302 input . type === "date" &&
303303 input_options . message . date
304304 ) {
305- this . set_validity ( { input : input , msg : input_options . message . date } ) ;
305+ this . set_error ( { input : input , msg : input_options . message . date } ) ;
306306 } else if (
307307 validity_state . rangeUnderflow &&
308308 input . type === "datetime" &&
309309 input_options . message . datetime
310310 ) {
311- this . set_validity ( { input : input , msg : input_options . message . datetime } ) ;
311+ this . set_error ( { input : input , msg : input_options . message . datetime } ) ;
312312 } else if (
313313 validity_state . rangeOverflow &&
314314 input . type === "datetime" &&
315315 input_options . message . datetime
316316 ) {
317- this . set_validity ( { input : input , msg : input_options . message . datetime } ) ;
317+ this . set_error ( { input : input , msg : input_options . message . datetime } ) ;
318+ } else {
319+ // Still an error, but without customized messages.
320+ // Call `emit_update` separately
321+ this . emit_update ( "invalid" ) ;
318322 }
323+
319324 }
320325
321326 if ( event ?. type === "submit" ) {
@@ -327,7 +332,7 @@ class Pattern extends BasePattern {
327332 this . set_error_message ( input ) ;
328333 }
329334
330- set_validity ( { input, msg, attribute = null , min = null , max = null } ) {
335+ set_error ( { input, msg, attribute = null , min = null , max = null , skip_event = false } ) {
331336 // Replace some variables, as like validate.js
332337 if ( attribute ) {
333338 msg = msg . replace ( / % { attribute} / g, attribute ) ;
@@ -345,9 +350,13 @@ class Pattern extends BasePattern {
345350 // Hidden inputs do not participate in validation but we need this
346351 // (e.g. styled date input).
347352 input [ KEY_ERROR_MSG ] = msg ;
353+
354+ if ( ! skip_event ) {
355+ this . emit_update ( "invalid" ) ;
356+ }
348357 }
349358
350- remove_error ( input , all_of_group = false ) {
359+ remove_error ( input , all_of_group = false , skip_event = false ) {
351360 // Remove error message and related referencesfrom input.
352361
353362 let inputs = [ input ] ;
@@ -370,10 +379,15 @@ class Pattern extends BasePattern {
370379 }
371380 }
372381 }
382+
383+ if ( ! skip_event ) {
384+ this . emit_update ( "valid" ) ;
385+ }
373386 }
374387
375388 set_error_message ( input ) {
376- this . remove_error ( input ) ;
389+ // First, remove the old error message.
390+ this . remove_error ( input , false , true ) ;
377391
378392 // Do not set a error message for a input group like radio buttons or
379393 // checkboxes where one has already been set.
0 commit comments