title: How to remove FacesMessages from the FacesContext?
body: <p>In my screen, I have a drop down(select box), on selection of any of the options in that drop down , i display one or more text boxes beside the select box using javascript/css - display:none and display:block. All these input controls are in the same jsf form. Each of the input controls have their own validator. The problem is suppose the user selects <em>option1</em> from selection box and doesn't input value or inputs a wrong value for <em>inputbox1,</em> i add a custom FacesMessage in the Validator and is shown appropriately and suppose the user selects the <em>option2</em> the second time and inputs the wrong value for the <em>inputbox2</em> then another FacesMessage is added in the validator. But <strong>Now both the Messages are shown - means - the message for inputbox1 and inputbox2 - which is wrong</strong> My assumption is that , this happens because they exist in the same form and their instances are not destroyed yet in the FacesContext and in the UIView. I decided to delete the messages this way </p>
  
  <pre><code>Iterator&lt;FacesMessage&gt; msgIterator = FacesContext.getCurrentInstance().getMessages();
      while(msgIterator.hasNext())
      {
          msgIterator.next();
          msgIterator.remove();
      }
  </code></pre>
  
  <p>But this sometimes gives java.util.NoSuchElementException org.apache.myfaces.shared_impl.renderkit.html.HtmlMessagesRendererBase$MessagesIterator.next</p>
  
  <p>So 2 questions :</p>
  
  <p>1) What is the problem in deleting the FacesMessages this way? I am using myfaces-api-1.2.3.jar and myfaces-impl-1.2.3.jar</p>
  
  <p>2) Is there a better approach to handle my scenario? I only want to show relevent messages every time a jsf request is processed</p>
  
  <p>Thanks</p>