Home > JSF > How To Skip JSF Validation When Required

How To Skip JSF Validation When Required

Sometimes it may require actions which have to get executed without validation or after partial validation. You may find dozen dozens of posts in various forums, blogs regarding skipping JSF validation. But very often we find a satisfactory result. I don’t want to mean we will not find right solution or we do not solve the problem, what I mean we can solve the problem but after lots of tweak, lots of $$ time spending.

Think I have a simple form with some input text components with required attribute true and I am going to refresh this form with new set of values by clicking a command button. How much extra labor I have to pay to solve this? Ok forget this scenario now think I have a form with some input text and dropdown components with required attribute true and I want to populate one dropdown component depends on another and even one dropdown component may not need to validate at all where as another one may need to validate the data based on which it will be populated. Again do you think how much extra labor I have to pay to solve this and even should I do this extra work? Hmm, I Know I have no easy way to work around this problem. Here I will share my thoughts which may help you finding an easy solution to work around the problems that I was discussed above.

First agree with me that we do not make command, dropdown or any other component immediate for skipping validation. Honestly most of the cases immediate attribute does not help you to solve your problem rather it insists you to do extra work.

An easy solution is to do something like below:

<!-- text1 need to be validated only when command button 'frmName:btnSave' will be pressed -->
<h: inputText id=”text1” value=”” required=”#{!empty param[‘frmName:btnSave’]}” />

If you want to execute an action without any validation check like after pressing “cancel”, “refresh”, “clear all” button, you can follow above solution. But to solve more complicated scenario like the second scenario that I discussed at the beginning of this post we will have to do some more work.

First group your input components (whose data need to be validated at validation phase) based on component validation requirement before an action execution and action execution order (think one group of components may need to be validated before executing an action, same group may need not to be validated before executing another action), make group as large as possible so that same component does not belongs to two group and think a name of each group like requireValidationGrp1, requireValidationGrp2 etc. Now use this group as request parameter name inside value expression of required attribute. See below:

<!-- text1 need to be validated only when request parameter 'requireValidationGrp1' has value 1 -->
<h: inputText id=”text1” value=”” required=”#{param[‘ requireValidationGrp1’]==’1’}” />
<!-- text2 need to be validated only when request parameter 'requireValidationGrp2' has value 1 -->
<h: inputText id=”text2” value=”” required=”#{param[‘ requireValidationGrp2’]==’1’}” />
<!-- we do not need to validate text3 at all -->
<h: inputText id=”text3” value=”” required=”false” />

So one or more group of input components will be validated in validation phase based on some request parameters. Now who push these parameters, when and why? Obviously we will push these parameter with value 1 using JSF tags inside components which has action and which will be executed on validated group of components data. That is how we can control which group of components will be validated in validation phase. See below:

<!--don’t require any validation and hence we don't need to push any request parameter. you can apply this for buttons like cancel, refresh, clear all etc. -->
<h:commandButton value="Clear All"></h:commandButton>
<!-- require validation of two groups: requireValidationGrp1 and requireValidationGrp2 after pressing save button -->
<h:commandButton value="Save">
<f:param name="requireValidationGrp1" value="1"></f:param>
<f:param name="requireValidationGrp2" value="1"></f:param>
<!-- require validation of only one group: requireValidationGrp1 after pressing copy button -->
<h:commandButton value="Copy">
<f:param name="requireValidationGrp1" value="1"></f:param>

See below for a complete example:

<h:panelGroup id="region1" layout="block" style="width: 100%;height: 100%">
<a4j:form id="frm1">
<rich:panel id="panel1" style="width:80%;">
<f:facet name="header">Skipping validation when required</f:facet>
<h:panelGrid columns="1">
<h:panelGrid columns="3" id="grid1">
<h:outputLabel for="input1" value="Input1: "></h:outputLabel>
<h:inputText id="input1" required="#{param['requireValidationGrp2'] == '1'}" requiredMessage="input1 required"></h:inputText>
<rich:message for="input1" />
<h:outputLabel for="input2" value="Input2: "></h:outputLabel>
<h:inputText id="input2" required="#{param['requireValidationGrp1'] == '1'}" requiredMessage="Input2 required"></h:inputText>
<rich:message for="input2" />
<h:outputLabel for="input3" value="input3"></h:outputLabel>
<h:selectOneMenu id="input3" value="0">
<f:selectItem itemLabel="Select" itemValue="0"/>
<f:selectItem itemLabel="s1" itemValue="1"/>
<f:selectItem itemLabel="s2" itemValue="2"/>
<f:selectItem itemLabel="s3" itemValue="3"/>
<f:selectItem itemLabel="s4" itemValue="4"/>
<a4j:support event="onchange" reRender="region1">
<a4j:actionparam name="requireValidationGrp1" value="1">
<rich:message for="input3" />
<h:panelGroup id="btn1" style="float:left;" layout="block">
<a4j:commandButton value="Save" reRender="region1">
<a4j:actionparam name="requireValidationGrp1" value="1">
<a4j:actionparam name="requireValidationGrp2" value="1">
<h:panelGroup id="btn3" style="float:left" layout="block">
<a4j:commandButton type="button" value="Clear All" reRender="region1">

  1. Johnf161
    May 15, 2014 at 3:26 pm

    You could certainly see your skills within the work you write. The world hopes for more passionate writers like you who arent afraid to say how they believe. Always follow your heart. beeekacdefck

  2. May 23, 2013 at 8:36 pm

    Howdy! Do you know if they make any plugins to help with SEO?

    I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good success.
    If you know of any please share. Kudos!

  3. November 28, 2012 at 1:11 pm

    Hello there, I found your blog via Google while looking for a comparable topic, your site came up, it seems great. I have added to favourites|added to bookmarks.

  4. flyingbear
    May 2, 2012 at 7:45 am

    Nice approach. But don’t you thing that this is a security issue? Parameters are passed by http request and are editable by the client. This way the client may skip the validation by adding / removing request parameters.

    • May 2, 2012 at 8:45 am

      Very very good point, that’s true. However, we should not thrust client side validation at all!

  5. Alam
    April 28, 2012 at 9:59 am

    Below code not working for me.

  6. Alam
    April 28, 2012 at 9:58 am

    below cod e not working for me.

    what is the frmName?

  7. March 12, 2012 at 8:52 am

    Interesting and a very decent solution! thanks for sharing! 🙂

  8. Miki
    August 13, 2011 at 12:51 am

    You are great, thank you for solution.

  9. Mohamed Zayadi
    July 30, 2011 at 1:20 pm

    thank you for the great souliotion.

  10. André Mello
    July 7, 2011 at 4:08 pm

    It helped me very much, thanks! Straight, the way I like it!

  11. Alleksejs
    March 29, 2011 at 1:59 am

    Thank you !!

  12. Alleksejs
    March 29, 2011 at 1:59 am

    Thank you,

  13. yves
    March 9, 2011 at 1:17 pm

    Nice! 🙂

  14. March 5, 2011 at 4:29 pm

    Wise. Thanks!

  15. Paulo
    November 24, 2010 at 1:03 pm

    Brilliant solution, this tip should be included in the JSF documentation 🙂
    Thank you very very much

  16. Gabriel
    November 17, 2010 at 7:44 pm

    Thanks, excellent post, the first solution worked perfectly for me

  17. Dheeraj Sisodia
    November 15, 2010 at 6:36 am

    Smart Solution , Thanks a lot………

  18. Javi
    October 28, 2010 at 1:43 pm

    Thanks!!! 😉 Good tip!

  19. Christoph
    August 5, 2010 at 8:54 am

    oh well.. my code was stripped out =( maybe I can obscure it a bit:

    oo h:selectOneMenu id=”countrySelectBox” required=”true” oo
    oo f:selectItems value=”#{myBean.countries}” /oo
    oo a4j:support immediate=”true” ajaxSingle=”true”oo
    oo f:setPropertyActionListener target=”#{suggestionHandler.selectedCountry}” value=”#{param[rich:clientId(countrySelectBox)]}” /oo
    /oo a4j:support oo
    /oo h:selectOneMenu oo

    hopefully this will help somone,
    best regards

  20. Christoph
    August 5, 2010 at 8:48 am

    Thanks for the article, it pointed me towards the right direction for my own solution: In my case, I can’t use the id of the submit buttons (because it is inserted via facelet templating).

    My Solution looks like this (using jsf 1.2 and richfaces 3.3.2):

    the setPropertyActionListener reads the current value of the selectOneMenu from the parameter map of the current request, which is triggered onchange via the a4j:support.
    It works because the whole form is always submitted to the server (regardless of ajaxSingle), so the parameter map also contains the current values of all fields of the form and can be accessed as needed 🙂 (regardless of required=true and immediate=true).

  21. Marjan
    June 3, 2010 at 11:34 am

    Great job!!! Thank you 🙂

  22. June 2, 2010 at 1:09 pm

    Really great trick, thank you so much !!

  23. Emilio
    March 31, 2010 at 6:02 pm

    Thanks for the tips, you saved my day 😉

  1. September 23, 2010 at 2:36 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: