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   * @author Leonardo Uribe (latest modification by $Author: struberg $)
35   * @version $Revision: 1188235 $ $Date: 2011-10-24 12:09:33 -0500 (Mon, 24 Oct 2011) $
36   * @since 2.0
37   */
38  @JSFValidator(name = "f:validateRequired",
39          bodyContent = "empty")
40  @JSFJspProperty(name = "binding", 
41          returnType = "javax.faces.validator.RequiredValidator",
42          longDesc = "A ValueExpression that evaluates to a RequiredValidator.")
43  public class RequiredValidator implements Validator
44  {
45  
46      // FIELDS
47      public static final String VALIDATOR_ID = "javax.faces.Required";
48  
49      // CONSTRUCTORS    
50      public RequiredValidator()
51      {
52      }
53  
54      // VALIDATE
55      public void validate(FacesContext facesContext, UIComponent uiComponent,
56              Object value) throws ValidatorException
57      {
58          if (facesContext == null)
59          {
60              throw new NullPointerException("facesContext");
61          }
62          if (uiComponent == null)
63          {
64              throw new NullPointerException("uiComponent");
65          }
66  
67          //Check if the value is empty like UIInput.validateValue
68          boolean empty = value == null
69                  || (value instanceof String && ((String) value).length() == 0);
70  
71          if (empty)
72          {
73              if (uiComponent instanceof UIInput)
74              {
75                  UIInput uiInput = (UIInput) uiComponent;
76                  if (uiInput.getRequiredMessage() != null)
77                  {
78                      String requiredMessage = uiInput.getRequiredMessage();
79                      throw new ValidatorException(new FacesMessage(
80                              FacesMessage.SEVERITY_ERROR, requiredMessage,
81                              requiredMessage));
82                  }
83              }
84              throw new ValidatorException(_MessageUtils.getMessage(facesContext,
85                      facesContext.getViewRoot().getLocale(),
86                      FacesMessage.SEVERITY_ERROR, UIInput.REQUIRED_MESSAGE_ID,
87                      new Object[] { _MessageUtils.getLabel(facesContext,
88                              uiComponent) }));
89          }
90      }
91      
92      @JSFProperty(faceletsOnly=true)
93      private Boolean isDisabled()
94      {
95          return null;
96      }
97      
98      @JSFProperty(faceletsOnly=true)
99      private String getFor()
100     {
101         return null;
102     }
103 }