aspose file tools*
The moose likes JSF and the fly likes display error  message Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "display error  message" Watch "display error  message" New topic
Author

display error message

lynn fann
Ranch Hand

Joined: Oct 15, 2006
Posts: 115
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?





Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16137
    
  21

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
lynn fann
Ranch Hand

Joined: Oct 15, 2006
Posts: 115

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

Joined: Jun 25, 2001
Posts: 16137
    
  21

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

Joined: Oct 15, 2006
Posts: 115
"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

Joined: Jun 25, 2001
Posts: 16137
    
  21

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

Joined: Oct 15, 2006
Posts: 115
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

Joined: Jun 25, 2001
Posts: 16137
    
  21

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

Joined: Oct 15, 2006
Posts: 115
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

Joined: Oct 15, 2006
Posts: 115
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

Joined: Jun 25, 2001
Posts: 16137
    
  21

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: display error message