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  
20  package org.apache.myfaces.tobago.example.demo;
21  
22  import javax.enterprise.context.RequestScoped;
23  import javax.faces.application.FacesMessage;
24  import javax.faces.component.UIComponent;
25  import javax.faces.component.UIInput;
26  import javax.faces.context.FacesContext;
27  import javax.faces.validator.ValidatorException;
28  import javax.inject.Named;
29  import java.io.Serializable;
30  
31  @RequestScoped
32  @Named
33  public class ValidationController implements Serializable {
34    private String letter;
35  
36    public String getLetter() {
37      return letter;
38    }
39  
40    public void setLetter(final String letter) {
41      this.letter = letter;
42    }
43  
44    public void customValidator(final FacesContext facesContext, final UIComponent component, final Object value)
45            throws ValidatorException {
46      if (value == null) {
47        return;
48      }
49      if (!"tobago".equalsIgnoreCase(value.toString())) {
50        throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Please type in 'Tobago'",
51                "Please type in 'Tobago'"));
52      }
53    }
54  
55    public void passwordValidator(final FacesContext facesContext, final UIComponent component, final Object value)
56        throws ValidatorException {
57      final String password = value.toString();
58  
59      final UIInput confirmationField = (UIInput) component.getAttributes().get("confirmationField");
60      final String confirmationFieldValue = confirmationField.getSubmittedValue().toString();
61  
62      if (password.isEmpty() || confirmationFieldValue.isEmpty()) {
63        return;
64      }
65  
66      if (!password.equals(confirmationFieldValue)) {
67        confirmationField.setValid(false);
68        throw new ValidatorException(new FacesMessage("Passwords must match."));
69      }
70    }
71  }