Doing more with <h:messages/>

I want to do more with my <h:messages/> I need some help because i'm not comming up with anything real creative here..

Here is the example;

<h:messages layout="table"/>

<h:form id="register">

<div id="wrapper" class="clearfix" >

<div id="maincol" >

<h:panelGrid columns="2">

<h:outputLabel value="Username"/>

<h:inputText required="true" id="username" value="#{regHandler.username}"/>

<h:outputLabel value="Password"/>

<h:inputSecret required="true" id="password" value="#{regHandler.password}"/>

<h:outputLabel value="Confirm Password"/>

<h:inputSecret required="true" id="confPassword" value="#{regHandler.confirmPassword}"/>

<h:outputLabel value="First Name"/>

<h:inputText required="true" id="first" value="#{regHandler.firstName}"/>

<h:outputLabel value="Last Name"/>

<h:inputText required="true" id="last" value="#{regHandler.lastName}"/>

<h:outputLabel value="E-mail"/>

<h:inputText required="true" id="email" value="#{regHandler.email}"/>

<h:graphicImage value="WeakAuth"/>

<h:inputText required="true" id="captcha" value="#{regHandler.weakAuthinator}"/>

</h:panelGrid>

<h:commandButton value="Register" action="#{regHandler.doRegister}"/>

</div>

</div>

</h:form>

Now here is the problem, If I submit this form with out filling out any of the inputs I will get a nice table of

Validation Error: Value is required.

Validation Error: Value is required.

Validation Error: Value is required.

Validation Error: Value is required.

Validation Error: Value is required.

Validation Error: Value is required.

Validation Error: Value is required.

In addition to these errors I would also like to highlight the input(s) that failed validation. Does anyone know how I can approach this?

Thanks in advance,

t

[2979 byte] By [skepticala] at [2007-11-27 6:54:16]
# 1

hi ,

u can write user specific errror messages according to ur requirements.

Like u can use some Validators (may be with Reg Expressions)

and register these validators in faces.config.xml and User specific messages u can write in properties file and register tht resource bundle in faces config.

It'll work.

try for tht.

Roshu_Roshua at 2007-7-12 18:29:14 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Did you understand the question?Well, for such cases I'd rather to use componentbinding instead of valuebinding so that you have more control over the components from the backing bean, i.e. setting the styles.
BalusCa at 2007-7-12 18:29:14 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

If you used the component binding, would all your validation be inside the bean? I can't say I'm a big fan of component binding.

The only real good solution I've come up with is a handing the validation in the bean and manipulating the components style when validation has failed.

-t

skepticala at 2007-7-12 18:29:14 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

No, that is certainly not true. You can define validators in JSF. And why aren't you a big fan of componentbinding? Is that because you don't fully understand it or do you have had negative experiences?

Talking about defining validators in JSF, another solution comes to mind .. You can also write a custom validator and assign it to the JSF components. One of the arguments passed through the validator is the UIComponent itself. Use it to set the style.

BalusCa at 2007-7-12 18:29:14 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

When I said I wasn't a fan of component binding I was meaning for this solution. I totally understand how component binding works and use it frequently especially with datatables and it is a very cool feature of JSF.I just don't want to have to bind every one of these components (there are lots of them) just to change the style.

I really like the suggestion of defining the validator in JSF. I'm going to pursue that and will post my results.

Thanks for the suggestions and comments.

skepticala at 2007-7-12 18:29:14 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
> I just don't want to have to bind> every one of these components (there are lots of> them) just to change the style. I said, you can use them *instead* of valuebindings.But go ahead with custom validators anyway :)
BalusCa at 2007-7-12 18:29:14 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

It works rather well.

I opted to use the form of an inline custom validators. Here is the important code in case anyone else ever needs it. I could have just as easy made a custom validator class and used the non-inline form of validation, but I opted for this methodology because it was the easiest to implement. (Yeah I know lazy)

For inline custom validators:

Register your custom validator bean in your faces-config.xml

<managed-bean>

<managed-bean-name>validator</managed-bean-name>

<managed-bean-class>com.serviceposter.handlers.ValidatorHandler</managed-bean-class>

<managed-bean-scope>session</managed-bean-scope>

</managed-bean>

Next create your validation method:

public void validateEmail(FacesContext context,

UIComponent toValidate,

Object value) {

String email = (String) value;

if (email.indexOf('@') == -1) {

((UIInput)toValidate).setValid(false);

toValidate.getAttributes().put("styleClass","errorStyle");

FacesMessage message = new FacesMessage("Invalid Email address");

context.addMessage(toValidate.getClientId(context), message);

}

}

Now bind your new method to the input:

<h:inputText required="true" validator="#{validator.validateEmail}" id="email" value="#{regHandler.email}"/>

Finally create a css style to reflect the highlighting you want to use on the erroring component.

input.errorStyle {

background-color: red;

}

skepticala at 2007-7-12 18:29:14 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
Good job. Apart from that weak email address validation ;) Here you can find a regexp email address matcher: http://balusc.xs4all.nl/srv/dev-jep-use.html
BalusCa at 2007-7-12 18:29:14 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
Yup, thanks for the reference.
skepticala at 2007-7-12 18:29:14 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...