View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package javax.faces.validator;
20  
21  import javax.faces.application.FacesMessage;
22  import javax.faces.component.UIComponent;
23  import javax.faces.component.UIInput;
24  import javax.faces.context.FacesContext;
25  
26  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFJspProperty;
27  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty;
28  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFValidator;
29  
30  /**
31   * Check if a value is empty, in the same way as set UIInput required 
32   * property to true (including all rules related to this property).
33   * 
34   * @since 2.0
35   */
36  @JSFValidator(name = "f:validateRequired",
37          bodyContent = "empty")
38  @JSFJspProperty(name = "binding", 
39          returnType = "javax.faces.validator.RequiredValidator",
40          longDesc = "A ValueExpression that evaluates to a RequiredValidator.")
41  public class RequiredValidator implements Validator
42  {
43  
44      // FIELDS
45      public static final String VALIDATOR_ID = "javax.faces.Required";
46  
47      // CONSTRUCTORS    
48      public RequiredValidator()
49      {
50      }
51  
52      // VALIDATE
53      public void validate(FacesContext facesContext, UIComponent uiComponent,
54              Object value) throws ValidatorException
55      {
56          if (facesContext == null)
57          {
58              throw new NullPointerException("facesContext");
59          }
60          if (uiComponent == null)
61          {
62              throw new NullPointerException("uiComponent");
63          }
64  
65          //Check if the value is empty like UIInput.validateValue
66          boolean empty = value == null
67                  || (value instanceof String && ((String) value).length() == 0);
68  
69          if (empty)
70          {
71              if (uiComponent instanceof UIInput)
72              {
73                  UIInput uiInput = (UIInput) uiComponent;
74                  if (uiInput.getRequiredMessage() != null)
75                  {
76                      String requiredMessage = uiInput.getRequiredMessage();
77                      throw new ValidatorException(new FacesMessage(
78                              FacesMessage.SEVERITY_ERROR, requiredMessage,
79                              requiredMessage));
80                  }
81              }
82              throw new ValidatorException(_MessageUtils.getMessage(facesContext,
83                      facesContext.getViewRoot().getLocale(),
84                      FacesMessage.SEVERITY_ERROR, UIInput.REQUIRED_MESSAGE_ID,
85                      new Object[] { _MessageUtils.getLabel(facesContext,
86                              uiComponent) }));
87          }
88      }
89      
90      @JSFProperty(faceletsOnly=true)
91      @SuppressWarnings("unused")
92      private Boolean isDisabled()
93      {
94          return null;
95      }
96      
97      @JSFProperty(faceletsOnly=true)
98      @SuppressWarnings("unused")
99      private String getFor()
100     {
101         return null;
102     }
103 }