Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Changing appearance of form fields with validation errors

 
Wally Hartshorn
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When an input field fails validation, an error message can be displayed. That's good, but I would also like to change the appearance of the field that failed validation. Specifically, I would like to change the color of the border on the problem field(s) to red.

Unfortunately, I can't find any easy way to do this. I'm thinking of something (very roughly) like this...



...which I know wouldn't even come close to working, because "employee.validation" won't magically return "inputError" simply because "hours" failed the validator.

I have a feeling that the only way to do this is to write my own validator (which I haven't learned how to do yet) and have it set/clear a property of the bean (which sounds to me like a no-no).

Is there a standard/better way to change the appearance of a form field when it fails validation?
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would something like this work for you?
 
Wally Hartshorn
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that looks like it's exactly what I want -- except I can't get it to work.



Trying that, I get this error: "javax.faces.el.PropertyNotFoundException: Error getting property 'valid' from bean of type java.lang.Integer"

I noticed that in your example, you used "binding=" rather than "value=". Is that perhaps the source of my problem? I've never used "binding=" before, so I tried just changing "value=" to "binding=" and got an incomprehensible error message that simply confirmed that there's more to it than that.

Any suggestions?

Thanks!
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you will need to use binding because the valid() method exists in the component itself.

In your backing bean...



Then in your backing bean



Try that and see if it works out for you. Remember that you aren't getting and setting the value of a String/Integer/etc for maxDays anymore. You are binding it to an HtmlInputText component and will need to call the .value() method to actually get the input from the text field.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic