title: How to skip PreRenderView event when validation has failed?
body: <p>I have a use case in which I want to validate a bunch of viewParams and if I find validation errors, skip the preRenderView event and just display the error messages. Is this possible?</p>
  
  <p>e.g.</p>
  
  <pre><code>&lt;f:metadata&gt;
              &lt;f:viewParam id="param1" name="p1" value="#{someBean.param1}" /&gt;
              &lt;f:viewParam id="param2" name="p2" value="#{someBean.param2}" /&gt;
              &lt;f:event type="javax.faces.event.PostValidateEvent" listener="#{someBean.validateParams}" /&gt;
              &lt;f:event type="javax.faces.event.PreRenderViewEvent" listener="#{someBean.viewAction}" /&gt;
          &lt;/f:metadata&gt;
  </code></pre>
  
  <p>The method validateParams() is used to do multi-field validation and would probably go something like this:</p>
  
  <pre><code>    public void validateParams(ComponentSystemEvent event) {
         UIComponent source = event.getComponent();
  
         Integer value1 = (Integer) ((UIInput) source.findComponent("param1")).getLocalValue();
         Integer value2 = (Integer) ((UIInput) source.findComponent("param2")).getLocalValue();
  
         boolean valid1 = validOneField(value1);
         boolean valid2 = validOneField(value2);
         boolean valid12 = validBothFields(valid1, valid2);
  
         if(!valid1);//add facesMessage
         if(!valid2);//add facesMessage
         if(!valid12);//add facesMessage
  
         if (!valid1 || !valid2 || !valid12)
            FacesContext.getCurrentInstance().renderResponse();
      }
  </code></pre>
  
  <p>My understanding is that regardless of the forced render response, viewAction() would still be called. I could easily modify viewAction() to take the validation into account but I want to skip it completely due to other reasons (some messy code in that method that I rather not touch, etc.). Is there a way I could completely bypass viewAction() and display the validation error messages?</p>
  
  <p><strong>Edit: Using BalusC suggestion of FacesContext.getCurrentInstance().ValidationFailed()</strong></p>
  
  <p>i've revised validateParams() to:</p>
  
  <pre><code>    public void validateParams(ComponentSystemEvent event) {
         UIComponent source = event.getComponent();
  
         Integer value1 = (Integer) ((UIInput) source.findComponent("param1")).getLocalValue();
         Integer value2 = (Integer) ((UIInput) source.findComponent("param2")).getLocalValue();
  
         boolean valid1 = validOneField(value1);
         boolean valid2 = validOneField(value2);
         boolean valid12 = validBothFields(valid1, valid2);
  
         if(!valid1);//add facesMessage
         if(!valid2);//add facesMessage
         if(!valid12);//add facesMessage
  
         if (!valid1 || !valid2 || !valid12)
            FacesContext.getCurrentInstance().ValidationFailed();
            FacesContext.getCurrentInstance().renderResponse();
      }
  </code></pre>
  
  <p>and the modified viewAction() as follows:</p>
  
  <pre><code>public void viewAction(ComponentSystemEvent cse) {
      if(!FacesContext.getCurrentInstance().isValidationFailed()) {
          // viewAction() code
      }
  }
  </code></pre>
  
  <p>This seems to be working for me now. Hopefully, the view actions in JSF 2.2 won't require me to check whether validation failed since I believe they won't be called at all (the FacesContext.renderResponce() will skip invoke application step, which is where view actions are called).</p>