f:viewParam Validation Doesn't Fit With f:event

By , 22 June 2011

f:viewParam Validation Doesn't Fit With f:event

When I first head the JSF Expert Group talking about page parameters, I thought "great!". Fast forward a year and a half and after trying them out I got a bit of a surprise. My events were running even when validation failed which is quite different to a normal JSF postback where events are only run if the model is successfully validated and updated.

  <f:metadata>
    <f:viewParam label="Name" name="name" value="${subscribersBean.new.name}"/>
    <f:viewParam label="Email" name="email" value="${subscribersBean.new.email}" required="true">
      <web:validateEmail/>
    </f:viewParam>
    <f:event type="preRenderView" listener="${subscriptionsBean.subscribe}"/>
   </f:metadata>
f:viewParam Validation Doesn't Fit With f:event

The only suitable event available is preRenderView which is obviously going to be invoked regardless of the success or failure of the validations. The workaround is to check for the bean properties for null in the event since failed validation prevents the model being updated.

// if validation failed, model is not updated
if (getNew().getEmail() == null || getNew().getEmail().length() == 0) {
    return;
}

An additional caveat is the f:metadata and f:viewParam tags must be in the same facelets template and a direct child of f:view. It's annoying, but you can make use of <ui:define/> and <ui:insert/> to handle that.

About Roger Keays

f:viewParam Validation Doesn't Fit With f:event

I guess I'd call myself a problem solver. Either that, or I'm some sort of organic machine designed to convert oxygen into carbon dioxide. You could go either way on that. I'm into languages and stuff. I wrote a book. It's okay, I guess. What else? I like reading, swimming, eating, and playing music. Satisfied? Sheesh.

Leave a Comment

Please visit https://rogerkeays.com/f-viewparam-validation-doesn-t-fit-with-f-event to add your comments.