View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.html;
16  
17  /**
18   * An element that supports client-side validation using the Constraint Validation API.
19   *
20   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Constraint_validation">
21   *      Constraint Validation API</a>
22   *
23   * @author Ronald Brill
24   */
25  public interface ValidatableElement {
26  
27      /**
28       * Returns whether the element is a candidate for constraint validation.
29       *
30       * @return whether the element is a candidate for constraint validation
31       */
32      boolean willValidate();
33  
34      /**
35       * Sets the custom validity message for the element.
36       *
37       * @param message the new message
38       */
39      void setCustomValidity(String message);
40  
41      /**
42       * Returns whether the user has provided input that the browser is unable to convert.
43       *
44       * @return {@code true} if the user has provided input that the browser is unable to convert
45       */
46      default boolean hasBadInputValidityState() {
47          return false;
48      }
49  
50      /**
51       * Returns whether the element has a custom validity message.
52       *
53       * @return {@code true} if the element's custom validity message has been set to a non-empty
54       *         string by calling {@link #setCustomValidity(String)}
55       */
56      boolean isCustomErrorValidityState();
57  
58      /**
59       * Returns whether the element's value does not match the specified pattern.
60       *
61       * @return {@code true} if the value does not match the specified pattern;
62       *         if {@code true}, the element matches the {@code :invalid} CSS pseudo-class
63       */
64      default boolean hasPatternMismatchValidityState() {
65          return false;
66      }
67  
68      /**
69       * Returns whether the element's value does not satisfy the {@code step} constraint.
70       *
71       * @return {@code true} if the value does not fit the rules determined by the
72       *         {@code step} attribute; if {@code true}, the element matches the
73       *         {@code :invalid} and {@code :out-of-range} CSS pseudo-classes
74       */
75      default boolean isStepMismatchValidityState() {
76          return false;
77      }
78  
79      /**
80       * Returns whether the element's value exceeds the maximum allowed length.
81       *
82       * @return {@code true} if the value is longer than the maximum length specified by the
83       *         {@code maxlength} attribute; if {@code true}, the element matches the
84       *         {@code :invalid} CSS pseudo-class
85       */
86      default boolean isTooLongValidityState() {
87          return false;
88      }
89  
90      /**
91       * Returns whether the element's value is shorter than the minimum required length.
92       *
93       * @return {@code true} if the value is shorter than the minimum length specified by the
94       *         {@code minlength} attribute; if {@code true}, the element matches the
95       *         {@code :invalid} CSS pseudo-class
96       */
97      default boolean isTooShortValidityState() {
98          return false;
99      }
100 
101     /**
102      * Returns whether the element's value has a type mismatch.
103      *
104      * @return {@code true} if the value is not in the required syntax (for example,
105      *         for {@code email} or {@code url}); if {@code true}, the element matches the
106      *         {@code :invalid} CSS pseudo-class
107      */
108     default boolean hasTypeMismatchValidityState() {
109         return false;
110     }
111 
112     /**
113      * Returns whether the element's value exceeds the maximum allowed value.
114      *
115      * @return {@code true} if the value is greater than the maximum specified by the
116      *         {@code max} attribute; if {@code true}, the element matches the
117      *         {@code :invalid} and {@code :out-of-range} CSS pseudo-classes
118      */
119     default boolean hasRangeOverflowValidityState() {
120         return false;
121     }
122 
123     /**
124      * Returns whether the element's value is less than the minimum allowed value.
125      *
126      * @return {@code true} if the value is less than the minimum specified by the
127      *         {@code min} attribute; if {@code true}, the element matches the
128      *         {@code :invalid} and {@code :out-of-range} CSS pseudo-classes
129      */
130     default boolean hasRangeUnderflowValidityState() {
131         return false;
132     }
133 
134     /**
135      * Returns whether the element satisfies all validation constraints.
136      *
137      * @return {@code true} if the element is valid; if {@code true}, the element matches the
138      *         {@code :valid} CSS pseudo-class, otherwise the {@code :invalid} CSS pseudo-class
139      */
140     boolean isValidValidityState();
141 
142     /**
143      * Returns whether the element is missing a required value.
144      *
145      * @return {@code true} if the element has a required attribute but no value;
146      *         if {@code true}, the element matches the {@code :invalid} CSS pseudo-class
147      */
148     default boolean isValueMissingValidityState() {
149         return false;
150     }
151 }