I think, therefore I blog

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:viewParam label="Name" name="name" value="${subscribersBean.new.name}"/>
    <f:viewParam label="Email" name="email" value="${subscribersBean.new.email}" required="true">
    <f:event type="preRenderView" listener="${subscriptionsBean.subscribe}"/>
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) {

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. Since he left Australia in 2009, he has been living as a digital nomad in over 40 different countries around the world. Roger is addicted to surfing. His other interests are music, psychology, languages, and finding good food. Click here to subscribe to his weekly blog, or stalk him on Facebook and Twitter.

Leave a Comment

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

Join Over 1000 Subscribers

I write every Sunday about travel, psychology, and technology. Thousands of people just like you have already subscribed—and for good reason. It'll change your life. And it's free :)

Read a Good Book

“A spellbinding true story of love, passion and adventure. One can’t help but be swept away by 100% Love Guaranteed.” —Dr Tammie Matson, author of Elephant Dance.

Chat For A While

Your Vote Matters

Which animal will take over when humans go extinct?