5

How would you compare two string for equality in a JSF Validator?

if (!settingsBean.getNewPassword().equals(settingsBean.getConfirmPassword())) {
    save = false;
    FacesUtils.addErrorMessage(null, "Password and Confirm Password are not same", null);
}
Tiny
  • 26,031
  • 99
  • 315
  • 583
c12
  • 9,185
  • 45
  • 147
  • 248

1 Answers1

17

Use a normal Validator and pass the value of first component as attribute of second component.

<h:inputSecret id="password" binding="#{passwordComponent}" value="#{bean.password}" required="true"
    requiredMessage="Please enter password" validatorMessage="Please enter at least 8 characters">
    <f:validateLength minimum="8" />
</h:inputSecret>
<h:message for="password" />

<h:inputSecret id="confirmPassword" required="#{not empty passwordComponent.value}"
    requiredMessage="Please confirm password" validatorMessage="Passwords are not equal">
    <f:validator validatorId="equalsValidator" />
    <f:attribute name="otherValue" value="#{passwordComponent.value}" />
</h:inputSecret>
<h:message for="confirmPassword" />

(note that binding in above example is as-is; you shouldn't bind it to a bean property!)

with

@FacesValidator(value="equalsValidator")
public class EqualsValidator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        Object otherValue = component.getAttributes().get("otherValue");

        if (value == null || otherValue == null) {
            return; // Let required="true" handle.
        }

        if (!value.equals(otherValue)) {
            throw new ValidatorException(new FacesMessage("Values are not equal."));
        }
    }

}

If you happen to use JSF utility library OmniFaces, then you can use <o:validateEquals> for this. The exact case of "confirm password" is demonstrated on <o:validateEqual> showcase.

Community
  • 1
  • 1
BalusC
  • 1,040,783
  • 362
  • 3,548
  • 3,513
  • Hi BalusC, is there any other way to do this without binding the inputSecret component? – c12 Apr 03 '11 at 02:58
  • You could hardcode the ID and pass it instead. E.g. `` and then use `UIViewRoot#findComponent()` to get the component. However this is only clumsy. Why the aversion against binding? It doesn't make sense to me. – BalusC Apr 03 '11 at 02:59
  • Isn't there additional overhead (memory wise) when adding bindings for components? Its probably minimal, but just a question. – c12 Apr 03 '11 at 06:35
  • It's very minimal. It's just one more reference to an existing object in memory. Referencing a String is actually potentially more expensive since the value doesn't necessarily already exist in the string pool. – BalusC Apr 03 '11 at 11:07
  • @Med: JSF utility library OmniFaces has a reusable `` component which does exactly this: https://showcase-omnifaces.rhcloud.com/showcase/validators/validateEqual.xhtml – BalusC Jan 04 '13 at 11:21
  • I was just reading about OmniFaces on your blog. It's indeed a lot easier this way : a single tag in the view, and the job is done! I'm only starting to work with JSF, so I will keep homebrewing stuff, but I definitely keep this project in mind. – Med Jan 04 '13 at 16:42
  • When wondering why the validator is not working attach this to the faces-config.xml in the META-INF between the Tag equalsValidator com.yourpackage.EqualsValidator – Pwnstar Jan 11 '17 at 09:17
  • @ImproveAnything That only required when you're still using the jurassic JSF 1.x from nearly a decade ago when configuration annotations weren't that common. – BalusC Jan 11 '17 at 09:19