Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSF custom validation message

 
S Majumder
Ranch Hand
Posts: 349
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 349
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 349
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes , whats the problem ?
 
Mahendra Pratap
Ranch Hand
Posts: 42
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
S Majumder
Ranch Hand
Posts: 349
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic