JSF backing method and form validation - validation

<h:commandButton value="#{msg.signupbutton}" id="registernewuser"
action="registernewuser" execute="#form" update="reg"
style="color:white;background-image:none;background-color:#69A74E;" />
</h:panelGrid>
<h:outputLabel for="email" value="#{msg.emailLabel}: *" />
<p:inputText id="email" value="#{userBean.emailaddr}"
required="true" label="Email" validatorMessage="Invalid Email"
maxlength="32" match="emailtwo" >
<f:validateRegex
pattern="^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*#[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$"></f:validateRegex>
</p:inputText>
<h:message for="email" style="color:red"/>
<h:outputLabel for="emailtwo"
value="#{msg.confirmemailLabel}: *" />
<p:inputText id="emailtwo" label="Confirm Email"
value="#{userBean.emailTwo}" required="true"
requiredMessage="Email is required" maxlength="32" />
<h:message for="emailtwo" style="color:red"/>
Hi, I have this form where I need to check that the second email inputtext field is the same as that of the first email inputtext field. Kind of like the password match in primefaces.
On the first submission and if there are errors in other inputtext fields such as the username, the validation for the username will be done and the backing method registernewuser will not be run.
If there are no validation errors in other fields but only that emailTwo does not match that of email one, the method registernewuser will be run and the validation error will be shown on the form.
Is there a way to let the registernewuser to be executed even when there are errors in other fields? I have like to show the error message "Confirm email not the same as the first email" during the first form submission.
Primefaces 3.4.2
JSF: 2.1.29

Related

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
the content of the first input field to be validated for password strength and
the content of the second input field to be validated for equality with the content of the first field
How to achieve this?
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.
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

Primefaces password match on blur always validation error

Using primefaces password tag, I want to use the match as shown in the show case but with on blur validation, I use the following code:
<h:form id="formulaire">
<p:growl id="growl" showDetail="true" autoUpdate="true" />
...
<p:password id="pwd1" value="#{registrationBean.user.password}"
size="47" label="Password" required="true"
placeholder="Password*" >
<f:validateLength minimum="6" />
<p:ajax event="blur"/>
</p:password>
<br />
<p:password id="pwd2" value="#{registrationBean.user.password}"
size="47" label="Verification" required="true"
placeholder="Verification*" match="pwd1">
<p:ajax event="blur"/>
</p:password>
<br/>
<p:commandButton action="#{registrationBean.register}" value="register"/>
</h:form>
I use the match on the verification, to avoid displaying the error message
before trying to reproduce it.
the problem is that growl always displays the error message.
Even when I put the match on the first field, the growl error keeps showing.
Thank you for your help.
Edit:
The Best solution I managed to get is the following:
<p:password id="pwd1" value="#{registrationBean.password}"
size="47" label="Password" required="true"
placeholder="Password*" match="pwd2">
<f:validateLength minimum="6" />
</p:password>
<br />
<p:password id="pwd2" value="#{registrationBean.password}"
size="47" label="Verification" required="true"
placeholder="Verification*" >
<p:ajax process="pwd1 pwd2" event="blur"/>
</p:password>
the draw back is that there is no ajax verification after password is entered (length...), this is done only once the verification is entered.
(still looking for a better solution)

Custom validator not invoked when fields are empty

I have implemented a custom validator for my screen referring to the example given here.
But in my case, my validator method is not invoked if I don't enter any values in any of the field. So the check for empty fields is never done. The required messages are displayed everytime.
If I removed the required attributes, even in that case the validator is invoked only if the fields have some value in them. How do I get all my validations to be done in the validator class?
My xhtml:
<h:form>
<p:messages id="message" closable="true"/>
<h:panelGrid columns="3" id="changePassPanel">
<h:outputLabel for="pwd0" value="Current password :" />
<p:password id="pwd0" value="#{homeBean.password1}">
<f:validator validatorId="PasswordValidator"></f:validator>
<f:attribute name="pwd1" value="#{pwd1}" />
<f:attribute name="pwd2" value="#{pwd2}" />
</p:password>
<h:outputLabel for="pwd1" value="New password :" />
<p:password id="pwd1" value="#{homeBean.password2}" binding="#{pwd1}"/>
<h:outputLabel for="pwd2" value="Confirm new password :" />
<p:password id="pwd2" binding="#{pwd2}"/>
</h:panelGrid>
<p:commandButton id="saveButton" value="Save" validateClient="true"/>
</h:form>

JSF Validate on keyup event

i want to implement a validation for a username. The 'normal' way to do it
is something like that:
<h:outputText for="username" value="Username:" />
<p:inputText id="username" value="#{registerService.username}" >
<f:validator binding="#{usernameValidator}" />
</p:inputText>
<p:message for="username" />
<p:commandButton type="submit" value="Submit" action="#{registerService.addUser}" ajax="false" update="panel" validateClient="true" />
But what i now want to do is to validate the username everytime
the user presses a key in the username inputText without pressing
the commandButton.
So each character should trigger the validation and should update
the messages field for username to show the user instantly if the
username is already present in database or not.
How can i do that?
Found:
Ajax Listener
This triggers the validation!
<h:inputText id="foo" value="#{bean.foo}">
<f:ajax event="keyup" execute="#this" render="fooMessage" />
<f:validator validatorId="fooValidator" />
</h:inputText>
<h:message id="fooMessage" for="foo" />

How can I choose where the error message from the validator `required` appear?

How can I choose where the error message from the validator required appear?
The error message is now displayed at the bottom of my page but I want it next to my inputbox.
<p>Firstname: <h:inputText value="#{userBean.firstname}" required="true"/></p>
<p>Lastname: <h:inputText value="#{userBean.lastname}" required="true"/></p>
Those appearing at the bottom are actually unhandled messages which will only appear when the javax.faces.PROJECT_STAGE is been set to Development in web.xml.
You need a <h:message> to declare the location where the message should be shown. Further, you also need a <h:outputLabel> to represent the labels (which has semantic and usability advantages).
Here's the complete set:
<p>
<h:outputLabel for="firstname" value="Firstname:"/>
<h:inputText id="firstname" label="Firstname" value="#{userBean.firstname}" required="true"/>
<h:message for="firstname" />
</p>
<p>
<h:outputLabel for="lastname" value="Lastname:"/>
<h:inputText id="lastname" label="Lastname" value="#{userBean.lastname}" required="true"/>
<h:message for="lastname" />
</p>

Resources