title: Validation with JSF 2 only wanted on submit
body: <p>I have an issue with validation, as I only want validation to take place when the submit button is clicked on the screen, not when another button is clicked.</p>
  
  <p>In the displayed page option1.faces is the main file option1.xhtml, and several included files.  Below are fragments of code from the main page and two of the included files:</p>
  
  <p>Code in option1.xhtml:</p>
  
  <pre><code>&lt;h:inputText size="4" maxlen="5" id="teff1" value="#{option1.teff1}"&gt;
  &lt;f:validateDoubleRange minimum="#{option1.teff1Min}" maximum="#{option1.teff1Max}"
      disabled="#{simulator.validate}"/&gt;
  &lt;/h:inputText&gt;
  </code></pre>
  
  <p>Code in abundances0.xhtml that is included in option1.xhtml:</p>
  
  <pre><code>&lt;h:selectOneMenu id="abundanceSet0" value="#{abundance.abunSet0}" style="height:25px; width:180px;"&gt;
  &lt;f:selectItems value="#{abundance.abunSetMap}"/&gt;
  &lt;/h:selectOneMenu&gt;
  &lt;p:spacer width="37" height="0"/&gt;
  &lt;p:commandButton value="Select Set" actionListener="#{abundance.selectSet0}" update="abundances0"/&gt;
  </code></pre>
  
  <p>Code in footerButtons.xhtml that is included in option1.xhtml:</p>
  
  <pre><code>&lt;h:message for="teff1" style="color:red"/&gt;
  
  &lt;h:commandButton value="Submit" disabled="#{!login.loggedIn}" action="#{simulator.submit}" onclick="resetForm()"
  actionListener="#{simulator.validate}" class="button"/&gt;
  </code></pre>
  
  <p>The fragments of code from the corresponding beans are here:
  MyOption1Bean:</p>
  
  <pre><code>@ManagedBean(name="option1")
  @SessionScoped
  public class MyOption1Bean implements Serializable {
  
  // Lots of other private variables and objects
  
  private String teff1;
  
  private String teff1Min;
  private String teff1Max;
  
  // Option 1 constructor to initialze limits
  public MyOption1Bean() {
      ResourceBundle bundle = ResourceBundle.getBundle("com.csharp.validation");
      teff1Min = bundle.getString("teff1Min");
      teff1Max = bundle.getString("teff1Max");
  }
  
  public String getTeff1() {
      return teff1;
  }
  
  public void setTeff1(String teff1) {
      this.teff1 = teff1;
  }
  
  // Lots of getters, setters, methods, etc.
  }
  </code></pre>
  
  <p>MyAbundanceBean:</p>
  
  <pre><code>@ManagedBean(name="abundance")
  @SessionScoped
  public class MyAbundanceBean implements Serializable {
  
  // Lots of other private variables and objects
  
  
  public String getAbunSet0() {
      return abunSet[0];
  }
  
  public void setAbunSet0(String abunSet) {
      this.abunSet[0] = abunSet;
  }
  
  
  public Map&lt;String,String&gt; getAbunSetMap() {
      return abunSetMap;
  }
  
  
  public void selectSet0(ActionEvent e) {
      selectSet(0);
  }
  
  // Lots of getters, setters, methods, etc.
  
  }
  </code></pre>
  
  <p>MySimulatorBean:</p>
  
  <pre><code>@ManagedBean(name="simulator")
  @SessionScoped
  public class MySimulatorBean implements Serializable {
  
  // Lots of other private variables and objects
  
  private boolean validate;
  
  // When validate is true disabled is false so validation takes place.
  public boolean isValidate() {
      return !validate;
  }
  
  // When navigating away from the home page to one of the options, reset the error
  // and validate flags.
  public void resetError(ActionEvent event) {
      error = false;
      validate = false;
  }
  
  // On clicking "Submit" this enables the validate flag.
  public void validate(ActionEvent event) {
      validate = true;
  }
  
  // On clicking "Submit" this gets the user's input, and if succesful sends it to an output file then
  // navigate to a "Success" page, otherwise return to the original page.
  public String submit() {
  
      // Code to check for errors and output data to a file.
  
  }
  
  // Lots of getters, setters, methods, etc.
  }
  </code></pre>
  
  <p>In <strong>option1</strong> (the xhtml and the bean files) the user enters a value for <code>teff1</code>, which must be between <code>teff1Min</code> and <code>teff1Max</code>, which are obtained from a properties file.  This works correctly, and if a value for <code>teff1</code> is not given or is out of range, on clicking the "<strong>Submit</strong>" button, as given in the <code>footerButtons.xhtml</code>, the submit fails and and the <code>&lt;h:message/&gt;</code> tag displays an error. </p>
  
  <p>However, before clicking "<strong>Submit</strong>", if the input field for <code>teff1</code> is empty or has a wrong value,
  the <code>&lt;p:commandButton value="Select Set" .../&gt;</code> in the included <code>abundances0.xhtml</code> does not work.  It is supposed to update a display with a chosen menu, which otherwise it does.  I set the <code>immediate</code>
  attribute of <code>&lt;p:commandButton value="Select Set" /&gt;</code> to <code>true</code>, but it still does not work.  I only want the validation to take place when the "<strong>Submit</strong>" button is clicked, and nothing else.</p>
  
  <p>I tried an alternative way: where the flag <code>validate</code> in the simulator bean is used to disable the validation until it is wanted. Namely, when the <code>option1</code> page is visited it is <code>false</code>, to disabled is <code>true</code>, and no validation is done until the submit button is clicked, at which point it is set to <code>true</code>, so disabled is <code>false</code>.  Unfortunately, this dose not work, as JSF thinks the page is valid and navigates away from it before validation is performed.  This is in spite of the fact that <code>validate()</code> is executed before <code>submit()</code> in the simulator bean.  This is confirmed by inserting a print statement in each of them.</p>
  
  <p>Does anybody have any idea as to what is going on?, and is there an easy way of making sure that validation only takes place when the submit button is clicked?  Otherwise the display is locked up, and I'm unable to make the other buttons work.</p>
  
  <hr>
  
  <p>Many thanks for the clarification, and I did exactly what you suggested.  I did the following:</p>
  
  <p>First I put immdiate="true" in the command button that selects a menu in my abundances0.xhtml file:</p>
  
  <pre><code>&lt;p:commandButton value="Select Set" actionListener="#{abundance.selectSet0}" update="abundances0" immediate="true"/&gt;
  </code></pre>
  
  <p>then I changed the action in my abundance bean java file:</p>
  
  <pre><code>public void selectSet0(ActionEvent e) {
      selectSet(0);
      FacesContext.getCurrentInstance().renderResponse(); 
  }
  </code></pre>
  
  <p>but it still does not work.  If I click the button nothing happens unless a valid value is already in the input field for teff1 in the option1.xhtml file at the beginning.  I need this button to work, together with other ones like it, regardless of what is in the input field, until the submit button is clicked.  As far as I can see, I am doing everything correctly.</p>
  
  <p>Icidentally, I'm using JSF 2.0 with PrimeFaces 3.4.2 and Eclipse Indigo.</p>