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

Roger Keays is an artist, an engineer, and a student of life. He has no fixed address and has left footprints on 40-something different countries around the world. Roger is addicted to surfing. His other interests are music, psychology, languages, the proper use of semicolons, and finding good food.

Leave a Comment

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