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

Posted by Roger Keays, 22 June 2011, 11:20 AM

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>

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.

Add a comment

Please visit http://www.ninthavenue.com.au/f-viewparam-validation-doesn-t-fit-with-f-event to add your comments.