aspose file tools*
The moose likes JSF and the fly likes Change Component Style on Validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Change Component Style on Validation" Watch "Change Component Style on Validation" New topic
Author

Change Component Style on Validation

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I had asked this in another thread but thought it should be it's own question. I am using the standard required validator and displaying inline error messages when this validation fails. That causes some strange spacing issues in my form so what I'd prefer is to display a global error message at the top and then change the style of the inputText and outputText. So maybe the label turns red and the inputText turns a different color.

Anyway, I know how to do this manually with manual validation checks in the commandButton's action method. But I'd rather use the validation framework the way it was meant to be used. So I need some way of catching the fact that a validation occured and then manipulating my components in code.

Would I need some sort of phase listener to capture a thrown validation error or something? I am not clear on how to do this. Thanks.


GenRocket - Experts at Building Test Data
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Doing a bit of research on this it seems that a term for what I want would be called component blushing. Basically what I need to do and can't find the answer is determine in my backing bean or some listener WHEN an error occurs and at that point "blush" my component(s). So any ideas on that would be appreciated.
Varun Khanna
Ranch Hand

Joined: May 30, 2002
Posts: 1400
Originally posted by Gregg Bolinger:
I had asked this in another thread but thought it should be it's own question. I am using the standard required validator and displaying inline error messages when this validation fails. That causes some strange spacing issues in my form so what I'd prefer is to display a global error message at the top and then change the style of the inputText and outputText. So maybe the label turns red and the inputText turns a different color.

Anyway, I know how to do this manually with manual validation checks in the commandButton's action method. But I'd rather use the validation framework the way it was meant to be used. So I need some way of catching the fact that a validation occured and then manipulating my components in code.

Would I need some sort of phase listener to capture a thrown validation error or something? I am not clear on how to do this. Thanks.


I will recommend extending the standard validator. You can then play around with your components pretty easily and importantly it would be generic and framework level change, what you say ?


- Varun
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Varun Khanna:


I will recommend extending the standard validator. You can then play around with your components pretty easily and importantly it would be generic and framework level change, what you say ?


That sounds like a good idea. I'll look into that. I am also looking into registering a phaselistener with the lifecycle in my managed bean constructor and doing something there. We'll see.
Varun Khanna
Ranch Hand

Joined: May 30, 2002
Posts: 1400
Originally posted by Gregg Bolinger:


registering a phaselistener with the lifecycle in my managed bean constructor


How will you find the component(s) for which the validation failed? If you can find that information at this stage, I guess another option would be to use javascript onload in the BODY tag to read the h:messages tag and change the style at runtime.
But again am not sure how will you find the component(s) with wrong data and the corresponding label component(s).
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Varun Khanna:


How will you find the component(s) for which the validation failed? If you can find that information at this stage, I guess another option would be to use javascript onload in the BODY tag to read the h:messages tag and change the style at runtime.
But again am not sure how will you find the component(s) with wrong data and the corresponding label component(s).


Yeah, I just discovered that this isn't really going to work. Plus it is a lot of hack code. I'm going to try and look at creating a custom validator but that isn't the easiest in the world either. Both books don't really give good examples.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I know that I could just create a validate method in my backing bean and use that method as the components validator but as I said in my first post, I'd like to just let the required validator do what it does best and catch the validation errors at some point. I guess this isn't really possible with JSf or isn't easy. Tapestry does this automagically.
Varun Khanna
Ranch Hand

Joined: May 30, 2002
Posts: 1400
Originally posted by Gregg Bolinger:


Yeah, I just discovered that this isn't really going to work. Plus it is a lot of hack code. I'm going to try and look at creating a custom validator but that isn't the easiest in the world either. Both books don't really give good examples.


Custom validator should be *relatively simpler ...
an extra line, something like -->
Component.setStyle() //in case of error message.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Varun Khanna:


Custom validator should be *relatively simpler ...
an extra line, something like -->
Component.setStyle() //in case of error message.


Ok, makes sense. But how do I retrieve the label for the inputText component?
Varun Khanna
Ranch Hand

Joined: May 30, 2002
Posts: 1400
Originally posted by Gregg Bolinger:
I know that I could just create a validate method in my backing bean and use that method as the components validator but as I said in my first post, I'd like to just let the required validator do what it does best and catch the validation errors at some point. I guess this isn't really possible with JSf or isn't easy. Tapestry does this automagically.


What I do is create message at runtime "Value <@run_time_value> for the field <@run_time_id> is not valid."

you can get both the runtime parameters from the UIComponent object inside validator.
Varun Khanna
Ranch Hand

Joined: May 30, 2002
Posts: 1400
Originally posted by Gregg Bolinger:


Ok, makes sense. But how do I retrieve the label for the inputText component?


value of id attribute of your component should always be same as it's label
UIComponent.getId gives you the id
[ July 11, 2005: Message edited by: Varun Khanna ]
 
Consider Paul's rocket mass heater.
 
subject: Change Component Style on Validation