• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to disable bean validation on some fields in JSF 2 page?  RSS feed

 
Mohammad Noor Najdawi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

i am using JSF 2, and have a validation on my bean (JSR 303), the case as follow:
assume that my bean as follow:
public CarBean{

@NotNull(message = “The car name cannot be empty”)
private String carName;

@NotNull(message = “The car status cannot be empty”)
private String carStatus;

@NotNull(message = “The car number cannot be empty”)
private String carNumber;
.
.
setters and getters here
.
.
----------------------------------------------------------------------------------
and assume that i have 2 pages (AddCar.xhtml page and SearchCar.xhtml page)
now in my addCar.xhtml page the validation is work fine since the 3 fileds should field(shouldn't be empty) to add a car sucessfully; when i press ADD button.
but in SearchCar.xhtml page i have the 3 fields, may i want to search based on carName only and left the other fields empty, since all fields have @NOTNULL validation they will result an error ,
my question i want to keep the bean validation on my AddCar Screen, and want to disable it on SearchCar page or how to disable it on some fileds(ex: enable it on car name, and disable it on car number and car color in the SearchScreen.xhtml), can i do it? how?

Regards
 
Andrea Cerqueira
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this post is old, but I'm having the same problem, and maybe you can help me.
you solved this problem?
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Andrea!

Although we'd prefer that you start your own thread instead of digging up an old one, we'll forgive you. I'm puzzled why no one ever bothered to answer the original question, though.

One of the fundamental tenets of JSF is that when you submit a form, it's all-or-nothing. If even one input fails validation, the entire input is rejected and the actions and listeners don't fire.

If you're using annotation-based validation and the system is set up to employ them at the JSF View level, then these constraints get added to whatever explicit constraints you have defined on the View definition itself (for example, 'required="true"').

There are really only 2 ways to defeat this mechanism. You can either 1) remove the constraints from the target bean (which generally isn't an option) or 2) stage the inputs through backing bean-supplied properties that don't carry those constraints. Once a JSF action method is invoked, the target bean can be updated from the staging properties in the action method.
 
Andrea Cerqueira
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Tim.
As the post was exactly the same doubts that I had, I thought I should write it.
I understood what you said.
I will try with <h:inputText>
Thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!