Jens Piegsa
Jens Piegsa

Reputation: 7495

PrimeFaces password validation

I have some trouble understanding this PrimeFaces showcase:

<h:panelGrid columns="2" id="matchGrid" cellpadding="5">                   
    <h:outputLabel for="pwd1" value="Password 1: *" />
    <p:password id="pwd1" value="#{passwordView.password5}" match="pwd2" label="Password 1" required="true" />

    <h:outputLabel for="pwd2" value="Password 2: *" />
    <p:password id="pwd2" value="#{passwordView.password5}" label="Password 2" required="true" />
</h:panelGrid>

In particular, I do not understand, why the value binding of both input fields point to the same property password5.

If I follow this example, but add some validation for password strength

@StrongPassword
private String password5;

I get duplicated validation messages on this constraints (for both fields). This is not the intended behaviour, I'd expect

How to achieve this?

Upvotes: 1

Views: 2801

Answers (2)

espajava
espajava

Reputation: 81

The best way to validate two passwords to equals in Primefaces <p:password component is to use attribute match, you could put it to your main input password input form and indicate id of your second checking input form (Id of another password component to match value against - from Primefaces docs).

Let me show you an example:

<h:panelGrid columns="2" id="matchGrid" cellpadding="5">

    <p:outputLabel for="usPassword" value="Password:"/>

    <p:password id="usPassword" value="#{authBean.password}"
                required="true" placeholder="Password"
                requiredMessage="Error: enter your password"
                match="usPasswordConfirm"/>

    <p:outputLabel for="usPasswordConfirm" value="Repeat Password:"/>

    <p:password id="usPasswordConfirm" value="#{signupBacking.password}"
                required="true" placeholder="Repeat Password"
                requiredMessage="Error: repeat your password"/>
</h:panelGrid>

Here I used match="usPasswordConfirm" and the same id value has the second password input form, so Primefaces will check both typed values for a match

Upvotes: 0

Jens Piegsa
Jens Piegsa

Reputation: 7495

As there doesn't seem to be a need to record the second input in the view bean, I didn't add another property for it and just removed the value binding:

<p:password id="pwd2" label="Password 2" required="true" />

This gives the desired result. The content of the second field is also preserved on validation errors.

Upvotes: 1

Related Questions