title: How can I display ValidatorException and required="true" of same input field in different messages elements
body: <p>I took the following <a href="https://stackoverflow.com/a/7493527/617373">BalusC kickoff example</a> and modified it a bit by adding a submit button and additional h:messages and removing the <code>f:ajax</code> from the <code>h:inputSecret's</code> (removed the <code>f:ajax</code> cause for some reason when I leave the first <code>h:inputSecret</code> it immediately displays "value is required" error for the second <code>h:inputSecret</code> - but the user haven't got the chance to type it in... ???  &lt;- another future question ?:) )</p>
  
  <p><strong>OK, to make long story short:</strong> </p>
  
  <p>I'm trying to figure out how can display the validation errors regarding the both password fields(that the passwords are not equal) in the global h:messages and not in the individual h:message of the password fields
  I do want that the required="true" will be displayed in the <code>&lt;h:message</code> of each field...</p>
  
  <p>But right now the validation message (thrown by my exception) and the required="true" are being displayed in the same place </p>
  
  <p>Here is the code:
  </p>
  
  <pre><code>&lt;h:outputLabel for="password" value="Password:" /&gt;
  &lt;h:inputSecret id="password" value="#{bean.password}" required="true"&gt;
      &lt;f:validator validatorId="confirmPasswordValidator" /&gt;
      &lt;f:attribute name="confirm" value="#{confirmPassword.submittedValue}" /&gt;
  &lt;/h:inputSecret&gt;
  &lt;h:message id="m_password" for="password" /&gt;
  
  &lt;h:outputLabel for="confirm" value="Password (again):" /&gt;
  &lt;h:inputSecret id="confirm" binding="#{confirmPassword}" required="true"&gt;
  &lt;/h:inputSecret&gt;
  &lt;h:message id="m_confirm" for="confirm" /&gt;
  </code></pre>
  
  <p>And additional <code>h:commandButton</code> with <code>h:messages</code> below that code :</p>
  
  <pre><code>&lt;h:commandButton value="doSomething" action="#{myBean.myAction}"&gt;
      &lt;f:ajax execute="password confirm" render="m_password m_confirm"&gt;&lt;/f:ajax&gt;
  &lt;/h:commandButton&gt;
  &lt;h:messages globalOnly="true" styleClass="validation_value_required"/&gt;
  </code></pre>
  
  <pre class="lang-java prettyprint-override"><code>@FacesValidator("confirmPasswordValidator")
  public class ConfirmPasswordValidator implements Validator {
  
      @Override
      public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
          String password = (String) value;
          String confirm = (String) component.getAttributes().get("confirm");
  
          if (password == null || confirm == null) {
              return; // Just ignore and let required="true" do its job.
          }
  
          if (!password.equals(confirm)) {
              throw new ValidatorException(new FacesMessage("Passwords are not equal."));
          }
      }
  
  }
  </code></pre>
  
  <p>Also</p>
  
  <p>Thanks ahead,</p>
  
  <p><strong>Solution (Thanks to BalusC)</strong></p>
  
  <p>changed </p>
  
  <pre><code>&lt;f:attribute name="confirm" value="#{confirmPassword.submittedValue}" /&gt;
  </code></pre>
  
  <p>to</p>
  
  <pre><code>&lt;f:attribute name="confirm" value="#{confirmPassword}" /&gt;
  </code></pre>
  
  <p>and </p>
  
  <pre><code>String confirm = (String) component.getAttributes().get("confirm");
  </code></pre>
  
  <p>into</p>
  
  <pre><code>UIInput confirmPasswordComponent = (UIInput) component.getAttributes().get("confirm");
  String confirm = (String) confirmPasswordComponent.getSubmittedValue();
  </code></pre>
  
  <p>and </p>
  
  <pre><code>throw new ValidatorException(new FacesMessage("Passwords are not equal."));
  </code></pre>
  
  <p>into</p>
  
  <pre><code>context.addMessage(null, new FacesMessage("Passwords are not equal."));
  context.validationFailed();
  ((UIInput) component).setValid(false);
  confirmPasswordComponent.setValid(false);
  return;
  </code></pre>