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: lu4242 $)
35   * @version $Revision: 941095 $ $Date: 2010-05-04 18:33:22 -0500 (Tue, 04 May 2010) $
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              throw new NullPointerException("facesContext");
60          if (uiComponent == null)
61              throw new NullPointerException("uiComponent");
62  
63          //Check if the value is empty like UIInput.validateValue
64          boolean empty = value == null
65                  || (value instanceof String && ((String) value).length() == 0);
66  
67          if (empty)
68          {
69              if (uiComponent instanceof UIInput)
70              {
71                  UIInput uiInput = (UIInput) uiComponent;
72                  if (uiInput.getRequiredMessage() != null)
73                  {
74                      String requiredMessage = uiInput.getRequiredMessage();
75                      throw new ValidatorException(new FacesMessage(
76                              FacesMessage.SEVERITY_ERROR, requiredMessage,
77                              requiredMessage));
78                  }
79              }
80              throw new ValidatorException(_MessageUtils.getMessage(facesContext,
81                      facesContext.getViewRoot().getLocale(),
82                      FacesMessage.SEVERITY_ERROR, UIInput.REQUIRED_MESSAGE_ID,
83                      new Object[] { _MessageUtils.getLabel(facesContext,
84                              uiComponent) }));
85          }
86      }
87      
88      @JSFProperty(faceletsOnly=true)
89      private Boolean isDisabled()
90      {
91          return null;
92      }
93      
94      @JSFProperty(faceletsOnly=true)
95      private String getFor()
96      {
97          return null;
98      }
99  }