wood burning stoves 2.0*
The moose likes JSF and the fly likes JSF  custom validation message Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "JSF  custom validation message " Watch "JSF  custom validation message " New topic
Author

JSF custom validation message

S Majumder
Ranch Hand

Joined: Jun 03, 2009
Posts: 244
Hi every body I am doing some validation one text field , I want to show CUSTOM MESSAGE ,
like this :



In backing bean :



When I am not filling in this text field(First Name) ,my App is not showing the custom message ie:First Name is a required field

Can any body tell me , where I am doing mistake ?

thanks is advance ,
S
Kenneth Miller
Greenhorn

Joined: Jun 30, 2009
Posts: 7
Hi,
This section from http://www.ibm.com/developerworks/java/library/j-jsf3
should give you all you need.

Validation methods in backing beans

As an alternative to creating a separate validator class, you can simply implement custom validation in a backing bean method, as long as the method adheres to the same argument signature as the Validator interface's validate method. For instance, you might write the following method:

[SomeBackingBean.java]

public void validateEmail(FacesContext context,
UIComponent toValidate,
Object value) {
String email = (String) value;

if (email.indexOf('@') == -1) {
((UIInput)toValidate).setValid(false);

FacesMessage message = new FacesMessage("Invalid Email");
context.addMessage(toValidate.getClientId(context), message);
}

}



The method would then be used in the JSF tag via the validator attribute as shown here:

<h:inputText id="email"
value="#{UserRegistration.user.email}"
validator="#{UserRegistration.validateEmail}"
required="true">
</h:inputText>

Regards,
Kenneth
S Majumder
Ranch Hand

Joined: Jun 03, 2009
Posts: 244
Thanks for your reply ,

I applied your logic in my App , but the problem is validateFirstName method is not being called when the text field (first name) is empty .

What is the reason ?

If I write simple



below my text field it is showing the JSF build in custom message only .


regards,
S
Kenneth Miller
Greenhorn

Joined: Jun 30, 2009
Posts: 7
it should be fine as you have the required attribue set to to true. You're saying the action method associated with the form submit is called but your custom method isn't ?.
Regards,
Kenneth
S Majumder
Ranch Hand

Joined: Jun 03, 2009
Posts: 244
Yes , whats the problem ?
Mahendra Pratap
Ranch Hand

Joined: Feb 25, 2008
Posts: 42

Hi,

as you have required = true for your InputText that means you are using jsf inbuilt validation so it first validating through that only as it is failing the condition of required = true it is throwing validationException and aborting the next validations for that component.

if you provide some text in that field then your custom validator will get called.

Regards,
Mahendra


Mahendra
http://randomtechieblog.blogspot.com
S Majumder
Ranch Hand

Joined: Jun 03, 2009
Posts: 244
Thanks for your reply , so is there any way to show custom message for no value (NULL) in a text field ?

regards,
S
Mahendra Pratap
Ranch Hand

Joined: Feb 25, 2008
Posts: 42

yeah you can use either required = true or your custom validator.

I don't why you want to use custom validator for just checking whether there is some value or not when jsf already provided one for it.



Kenneth Miller
Greenhorn

Joined: Jun 30, 2009
Posts: 7
You can configure a mesage bundle in the faces-config.xml and add following entry to message bundle to override the input required message.

javax.faces.component.UIInput.REQUIRED=This field is required
Razvan Popovici
Greenhorn

Joined: Feb 15, 2007
Posts: 14
It seems validators are not triggered if the value is empty. So checking for emptiness with validators is pointless, they will never be reached.

For performance reasons it would be a better idea to perform all the non-session checking on the client side, using JavaScript (empty fields, emails are properly formed, ranges, names have at least two letters, etc ). In my opinion, the validator concept only makes sense for server side checking (for instance, looking to see if the entry is in the database).

Does anybody know how to trigger the text in Message controls from client sided JavaScript in an elegant fashion?
Siva Pokala
Greenhorn

Joined: May 15, 2009
Posts: 1


Hi,
Am new to jsf, am doing custom validation, i was unable to do validations for 5 fields at a time,using validator interface.
Can any one explain me with an example how to validate 5fields in a jsf page using validator interface.

Thanks ,
in advance.
 
 
subject: JSF custom validation message