Win a copy of Emmy in the Key of Code this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Changing appearance of form fields with validation errors

 
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?
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • 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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • 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.
 
And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!