1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }