View Javadoc
1   /*
2    * Copyright (c) 2011-2024 PrimeFaces Extensions
3    *
4    *  Permission is hereby granted, free of charge, to any person obtaining a copy
5    *  of this software and associated documentation files (the "Software"), to deal
6    *  in the Software without restriction, including without limitation the rights
7    *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    *  copies of the Software, and to permit persons to whom the Software is
9    *  furnished to do so, subject to the following conditions:
10   *
11   *  The above copyright notice and this permission notice shall be included in
12   *  all copies or substantial portions of the Software.
13   *
14   *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20   *  THE SOFTWARE.
21   */
22  package org.primefaces.extensions.component.inputotp;
23  
24  import java.util.List;
25  import java.util.Objects;
26  
27  import javax.faces.application.FacesMessage;
28  import javax.faces.application.ResourceDependency;
29  import javax.faces.context.FacesContext;
30  import javax.faces.convert.NumberConverter;
31  
32  import org.primefaces.component.api.AbstractPrimeHtmlInputText;
33  import org.primefaces.component.api.InputHolder;
34  import org.primefaces.component.api.RTLAware;
35  import org.primefaces.component.api.Widget;
36  import org.primefaces.component.inputtext.InputText;
37  import org.primefaces.extensions.component.inputphone.InputPhone;
38  import org.primefaces.extensions.util.Constants;
39  import org.primefaces.extensions.util.ExtLangUtils;
40  import org.primefaces.extensions.util.MessageFactory;
41  import org.primefaces.util.LangUtils;
42  
43  /**
44   * <code>InputOtp</code> component.
45   *
46   * @since 14.0.0
47   */
48  @ResourceDependency(library = "primefaces", name = "components.css")
49  @ResourceDependency(library = "primefaces", name = "jquery/jquery.js")
50  @ResourceDependency(library = "primefaces", name = "jquery/jquery-plugins.js")
51  @ResourceDependency(library = "primefaces", name = "core.js")
52  @ResourceDependency(library = Constants.LIBRARY, name = "inputotp/inputotp.css")
53  @ResourceDependency(library = Constants.LIBRARY, name = "inputotp/inputotp.js")
54  public class InputOtp extends AbstractPrimeHtmlInputText implements Widget, InputHolder, RTLAware {
55  
56      public static final String COMPONENT_TYPE = "org.primefaces.extensions.component.InputOtp";
57      public static final String COMPONENT_FAMILY = "org.primefaces.extensions.component";
58      public static final String DEFAULT_RENDERER = "org.primefaces.extensions.component.InputOtpRenderer";
59  
60      public static final String STYLE_CLASS = "ui-inputotp ui-widget";
61      public static final String RTL_STYLE_CLASS = "ui-inputotp-rtl";
62      public static final String CELL_STYLE_CLASS = "ui-inputotp-input " + InputText.STYLE_CLASS;
63      public static final String SEPARATOR_STYLE_CLASS = "ui-inputotp-separator";
64      public static final String INPUT_SUFFIX = "_input";
65      public static final String HIDDEN_SUFFIX = "_hidden";
66  
67      // disabled, readonly, style, styleClass, size, placeholder handled by component renderer
68      public static final List<String> INPUT_OTP_ATTRIBUTES_WITHOUT_EVENTS = List.of(
69                  "accesskey",
70                  "alt",
71                  "autocomplete",
72                  "dir",
73                  "lang",
74                  "inputmode",
75                  "tabindex",
76                  "title");
77  
78      // @formatter:off
79      @SuppressWarnings("java:S115")
80      public enum PropertyKeys {
81          placeholder,
82          autocomplete,
83          integerOnly,
84          inputStyle,
85          inputStyleClass,
86          separator,
87          ariaLabel,
88          length,
89          mask
90      }
91      // @formatter:on
92  
93      public InputOtp() {
94          setRendererType(DEFAULT_RENDERER);
95      }
96  
97      @Override
98      public String getFamily() {
99          return COMPONENT_FAMILY;
100     }
101 
102     @Override
103     public String getInputClientId() {
104         return getClientId() + INPUT_SUFFIX + 1;
105     }
106 
107     @Override
108     public String getValidatableInputClientId() {
109         return getClientId() + HIDDEN_SUFFIX;
110     }
111 
112     @Override
113     public String getLabelledBy() {
114         return (String) getStateHelper().get("labelledby");
115     }
116 
117     @Override
118     public void setLabelledBy(final String labelledBy) {
119         getStateHelper().put("labelledby", labelledBy);
120     }
121 
122     public String getPlaceholder() {
123         return (String) getStateHelper().eval(InputPhone.PropertyKeys.placeholder, null);
124     }
125 
126     public void setPlaceholder(final String placeholder) {
127         getStateHelper().put(InputPhone.PropertyKeys.placeholder, placeholder);
128     }
129 
130     @Override
131     public String getAutocomplete() {
132         return (String) getStateHelper().eval(PropertyKeys.autocomplete, "off");
133     }
134 
135     public boolean isIntegerOnly() {
136         return (Boolean) getStateHelper().eval(PropertyKeys.integerOnly, false);
137     }
138 
139     public void setIntegerOnly(final boolean integerOnly) {
140         getStateHelper().put(PropertyKeys.integerOnly, integerOnly);
141     }
142 
143     public String getInputStyle() {
144         return (String) getStateHelper().eval(PropertyKeys.inputStyle, null);
145     }
146 
147     public void setInputStyle(final String inputStyle) {
148         getStateHelper().put(PropertyKeys.inputStyle, inputStyle);
149     }
150 
151     public String getInputStyleClass() {
152         return (String) getStateHelper().eval(PropertyKeys.inputStyleClass, null);
153     }
154 
155     public void setInputStyleClass(final String inputStyleClass) {
156         getStateHelper().put(PropertyKeys.inputStyleClass, inputStyleClass);
157     }
158 
159     public String getSeparator() {
160         return (String) getStateHelper().eval(PropertyKeys.separator, null);
161     }
162 
163     public void setSeparator(final String separator) {
164         getStateHelper().put(PropertyKeys.separator, separator);
165     }
166 
167     public String getAriaLabel() {
168         return (String) getStateHelper().eval(PropertyKeys.ariaLabel, null);
169     }
170 
171     public void setAriaLabel(final String ariaLabel) {
172         getStateHelper().put(PropertyKeys.ariaLabel, ariaLabel);
173     }
174 
175     public int getLength() {
176         return (int) getStateHelper().eval(PropertyKeys.length, 4);
177     }
178 
179     public void setLength(final int length) {
180         getStateHelper().put(PropertyKeys.length, length);
181     }
182 
183     public boolean isMask() {
184         return (boolean) getStateHelper().eval(PropertyKeys.mask, false);
185     }
186 
187     public void setMask(final boolean mask) {
188         getStateHelper().put(PropertyKeys.mask, mask);
189     }
190 
191     @Override
192     protected void validateValue(FacesContext context, Object newValue) {
193         super.validateValue(context, newValue);
194         if (!isValid()) {
195             return;
196         }
197         String submittedValue = Objects.toString(getSubmittedValue(), org.primefaces.util.Constants.EMPTY_STRING);
198         if (LangUtils.isEmpty(submittedValue)) {
199             return;
200         }
201 
202         // all characters must match the length to be complete
203         if (isValid() && isRequired() && submittedValue.length() != getLength()) {
204             String requiredMessageStr = getRequiredMessage();
205             FacesMessage message;
206             if (null != requiredMessageStr) {
207                 message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
208                             requiredMessageStr,
209                             requiredMessageStr);
210             }
211             else {
212                 message = MessageFactory.getMessage(REQUIRED_MESSAGE_ID, FacesMessage.SEVERITY_ERROR,
213                             MessageFactory.getLabel(context, this));
214             }
215             context.addMessage(getClientId(context), message);
216             setValid(false);
217         }
218 
219         if (isValid() && isIntegerOnly()) {
220             boolean isDigit = ExtLangUtils.isDigitsOnly(submittedValue);
221             if (!isDigit) {
222                 setValid(false);
223                 String validatorMessage = getValidatorMessage();
224                 FacesMessage message;
225                 if (validatorMessage != null) {
226                     message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
227                                 validatorMessage,
228                                 validatorMessage);
229                 }
230                 else {
231                     String exampleValue = "9".repeat(getSize());
232                     message = MessageFactory.getMessage(NumberConverter.NUMBER_ID,
233                                 FacesMessage.SEVERITY_ERROR,
234                                 getSubmittedValue(),
235                                 exampleValue,
236                                 MessageFactory.getLabel(context, this));
237                 }
238                 context.addMessage(getClientId(context), message);
239             }
240         }
241     }
242 
243 }