This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Darcy DeClute's Scrum Master Certification Guide: The Definitive Resource for Passing the CSM and PSM Exams and have Darcy DeClute on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

display error message

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can i make the error messsage display out? I have the following code, but only the last error message appear. What do i have to change so that the error message for each field will be display out?





 
Saloon Keeper
Posts: 27493
195
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That isn't exactly the way I understand the message subsystem to work.

Unless you know something I don't, you'd normally code more like this:


First of all, the "id" attribute MUST be unique within its container and I'm not sure how you even get the page to compile otherwise. That's a basic restriction that's part of JSF's XML heritage.

Secondly, the "for=" attribute on a message is supposed to bind the message to the (unique) id of a specific control.

Thirdly, as in the example, required items are so fundamental that JSF is capable of handling them without any Java code at all, at least if you can endure the limitations in the generated message.

Typically you'd use the JSF context addMessage to enter messages that aren't tightly bound to specific controls and that would be presented by the <h:messages> element. Validation messages are normally created during the validation phase of the JSF lifecycle, and if a validation fails, that causes the lifecycle to be altered. None of the bean data will be updated and the action processor will not be invoked.
 
lynn fann
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


First of all, the "id" attribute MUST be unique within its container and I'm not sure how you even get the page to compile otherwise. That's a basic restriction that's part of JSF's XML heritage.


the "id" is my typo. i 4get to change them when copying. sorry for that..


I cant use "required=true", as i have 2 section for the user to chose to enter. The user just need to enter either sections. Within the section, there are serveral field that are mandatory. and why i did a validation check for null values in my managed bean is because the validator will not be invoked if it is a blank field.


Have another question, when i set the required=true on readonly field, it doesnt work, why is this so? Is this just how jsf set it?

 
Tim Holloway
Saloon Keeper
Posts: 27493
195
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

lynn fann wrote:
I cant use "required=true", as i have 2 section for the user to chose to enter. The user just need to enter either sections. Within the section, there are serveral field that are mandatory. and why i did a validation check for null values in my managed bean is because the validator will not be invoked if it is a blank field.

Have another question, when i set the required=true on readonly field, it doesnt work, why is this so? Is this just how jsf set it?



This is an area where JSF probably needs more work. I had to do a similar deal where I needed to check 2 copies of a password against each other. I did it, but I think the process for validating inter-dependent fields could be made simpler.

Rather than setting a message, you should implement a true JSF validator and throw the message as the associated text of a ValidationException. Don't try to make 1 validator do too many things, however - you only get one exception/message per validator.

"Required" means INPUT required, so putting it on a display-only control is sort of a contradiction in terms. Although I seem to remember having the same problem once.
 
lynn fann
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

"Required" means INPUT required, so putting it on a display-only control is sort of a contradiction in terms. Although I seem to remember having the same problem once.


Anyway to overcome this problem?

having anothe issue.. from my validator method, i throw a validatorException, but how do i print the messag on the page itself? When the exception is thrown, it will open a "HTTP500 error" page.
what do i need to do, so that the exception error is on the page itself.

here is the code for the validation. (Im not sure if i dont it correctly or not)


If i remove the line : throw new ValidatorException(message); the submit action will be trigger even though the validation fails.

 
Tim Holloway
Saloon Keeper
Posts: 27493
195
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as the "required" thing not applying to display-only, I'm not sure what you want.

If you throw a JSF ValidationException and you get a "500" page, either it's not a javax.faces.validator.ValidatorException or you didn't define your validator properly to JSF, because JSF is supposed to catch them and convert them to JSF context messages. If it does that, then the "message for=" element will print the exception message text.

In other words, if I create a JSF validator and register it with JSF and have it do this:



Then a page with with tags like this:


Would end up displaying something like this:

Date | 02/301/09 | Date must be in the form "MM/DD/YY"
 
lynn fann
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for the validator is in my managed bean, i did not create another validator class

from jsf page, i have the following:
<h:inputText id="userid" maxlength="16" value="#{myBean.userId}" validator="#{myBean.validateUserId}"/>
<h:message for="userid"/>

in my managed bean, i have the validation method.

Have another issues with validator. My submit action will be process, even when there is validation error.

Where have i went wrong?



 
Tim Holloway
Saloon Keeper
Posts: 27493
195
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your valididator's signature should be like this:

void validate(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.Object))

From what you've said, I assume that you are seeing it get called, but verify that fact.

If all else fails, put "immediate=true" on the inputText element.
 
lynn fann
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. thanks for your help, i managed to get it work. I try to throw the validatorException, and now it works.
thanks once again.
 
lynn fann
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
have another problem now. After processing the validator, it will just stop there. (There is no error/exception thrown from the validator) but my submit action is not being called

why is this so?

code of my validator
 
Tim Holloway
Saloon Keeper
Posts: 27493
195
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're catching and rethrowing the ValidatorException because your "catch" catches ALL Exceptions and ValidatorException is a subclass of Exception.

I don't really see any good reason to have a try/catch in that validator.
 
It will give me the powers of the gods. Not bad for a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic