ToolTips for error on validation

Community Driven Extensions Project
Post Reply
vineet
Posts: 387
Joined: 14 Oct 2011, 23:40

22 Mar 2012, 16:30

<h:panelGrid id="details" columns="2" columnClasses="formColumn1,formColumn2">
<pe:requiredLabel for="firstName" value="First Name: "/>
<p:inputText id="firstName" required="true" title="#{component.valid ? '' : 'First Name can not be empty'}"/>
<pe:requiredLabel for="lastName" value="Last Name: "/>
<p:inputText id="lastName" required="true" title="#{component.valid ? '' : 'Last Name can not be empty'}"/>
</h:panelGrid>

<p:commandButton value="Submit" process="details" update="details" style="margin-top: 10px;"/>

<pe:tooltip global="true" position="left center" targetPosition="right center" forSelector=".ui-state-error"/>

In this case if i need to handle error other validations like minimum length , datatype of field then how can i handle it ?

User avatar
Oleg
Expert Member
Posts: 3805
Joined: 02 Oct 2009, 09:41
Location: Germany, Black Forest

22 Mar 2012, 17:05

With attached validators

Code: Select all

<p:inputText id="lastName" required="true" title="#{component.valid ? '' : myBean.errorMessage}">
    <f:validateABC ..../>
    <f:validateXYZ ..../>
</p:inputText>
myBean.errorMessage should return last added (by any validator(s)) error message(s) which you can get in your bean as

Code: Select all

Iterator<FacesMessage> msgs = facesContext.getMessages("lastName");
Look in PF's MessageRenderer.java for more information.
PrimeFaces Cookbook (2. edition): http://ova2.github.io/primefaces-cookbook/ Learning Angular UI Development with PrimeNG: https://github.com/ova2/angular-develop ... th-primeng Blog: https://medium.com/@OlegVaraksin

vineet
Posts: 387
Joined: 14 Oct 2011, 23:40

22 Mar 2012, 18:22

Does it mean we will have to write custom validator for each validation which will call bean to validate errors like minimum length , required etc ?

User avatar
Oleg
Expert Member
Posts: 3805
Joined: 02 Oct 2009, 09:41
Location: Germany, Black Forest

22 Mar 2012, 18:50

No. You can use quite normally validators. In case of validation error, component set to invalid and the message is added to FacesContext as FacesMessage. You should try to get the added message in the bean.
PrimeFaces Cookbook (2. edition): http://ova2.github.io/primefaces-cookbook/ Learning Angular UI Development with PrimeNG: https://github.com/ova2/angular-develop ... th-primeng Blog: https://medium.com/@OlegVaraksin

vineet
Posts: 387
Joined: 14 Oct 2011, 23:40

22 Mar 2012, 21:56

But if i have a validation like required = "true" in such case bean is not being called then how do we add message from the bean ?

User avatar
Oleg
Expert Member
Posts: 3805
Joined: 02 Oct 2009, 09:41
Location: Germany, Black Forest

22 Mar 2012, 22:23

The bean is called when you update (render) input fields. If validation failed, the rendering phase is called. Input fields will be re-rendered in any case. As I said you can access messages in FacesContext from your bean. Maybe sogar in facelets direct (not tried)

Code: Select all

title="#{component.valid ? '' : facesContext.getMessages('lastName').next().getDetail()}"
Look into MessageRenderer.java as I said. You can copy code from there. Where is the problem?
PrimeFaces Cookbook (2. edition): http://ova2.github.io/primefaces-cookbook/ Learning Angular UI Development with PrimeNG: https://github.com/ova2/angular-develop ... th-primeng Blog: https://medium.com/@OlegVaraksin

User avatar
Oleg
Expert Member
Posts: 3805
Joined: 02 Oct 2009, 09:41
Location: Germany, Black Forest

22 Mar 2012, 22:29

I tried it special for you. It works. Here is the code:

XHTML

Code: Select all

<p:inputText id="lastName" required="true" label="Last Name" title="#{component.valid ? '' : myController.messageForLastName}"/>
Java (controller bean)

Code: Select all

public String getMessageForLastName() {
    return FacesContext.getCurrentInstance().getMessages("lastName").next().getDetail();
}
The same is for all fields and validators. You can also customize default message text in JSF validators. Standard JSF validators store messages under well-defined keys. Refer JSF docu please how to do this.
PrimeFaces Cookbook (2. edition): http://ova2.github.io/primefaces-cookbook/ Learning Angular UI Development with PrimeNG: https://github.com/ova2/angular-develop ... th-primeng Blog: https://medium.com/@OlegVaraksin

vineet
Posts: 387
Joined: 14 Oct 2011, 23:40

23 Mar 2012, 04:23

Thanks a lot ..Greatly appreciated :)

User avatar
Oleg
Expert Member
Posts: 3805
Joined: 02 Oct 2009, 09:41
Location: Germany, Black Forest

23 Mar 2012, 10:58

More generic and better if you use EL 2.x

Code: Select all

<p:inputText id="lastName" required="true" label="Last Name" title="#{component.valid ? '' : myController.getErrorMessage(component.clientId)}"/>

Code: Select all

public String getErrorMessage(final String clientId) {
    Iterator<FacesMessage> iter = FacesContext.getCurrentInstance().getMessages(clientId);
    if (iter.hasNext()) {
        return iter.next().getDetail(); // or getSummary()
    }

    return "";
}
I added this tip to my blog post http://ovaraksin.blogspot.de/2012/03/sh ... -with.html
PrimeFaces Cookbook (2. edition): http://ova2.github.io/primefaces-cookbook/ Learning Angular UI Development with PrimeNG: https://github.com/ova2/angular-develop ... th-primeng Blog: https://medium.com/@OlegVaraksin

Post Reply

Return to “Extensions”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 18 guests