When displaying a message with a JSF custom validator, the errorClass of h: message is not applied unless SEVERITY_ERROR is set in the message.

environment OS : macOS Hight Sierra Version10.13.2 Eclipse : Version: Neon.3 Release (4.6.3) GlassFish : 4.1.2 JSF : 2.2 JDK : 1.8

On this screen, click [Enter your password. ] I have set a custom validator in the text box.

Screen Shot 2017-12-26 at 23.03.17.png

inputPassword.xhtml


<abridgement>
<h:head>
  <title>input screen</title>
  <h:outputStylesheet library="css" name="base.css"/>
</h:head>
<body>
<h:form>
  <h:outputLabel>Please enter the password.</h:outputLabel>
  <br />
  <h:inputSecret id="password" value="#{passwordBean.password}">
    <f:validateRequired />
    <f:validateLength minimum="3" maximum="10" />
    <f:validator validatorId="passwordValidator" />
  </h:inputSecret>
  <h:message for="password" errorClass="error" />
  <br />
  <h:outputLabel>Already for confirmation&nbsp;&nbsp;Please enter it once.</h:outputLabel>
  <br />
  <h:inputSecret id="rePassword" value="#{passwordBean.kakuninPassword}">
    <f:validateRequired />
  </h:inputSecret>
  <h:message for="rePassword" errorClass="error" />
  <br />
  <h:commandButton value="Send" action="#{passwordBean.onClickSend}" />
</h:form>
</body>
</html>

base.css


@CHARSET "UTF-8";
.error {
    color: red;
}

If you do not set SEVERITY_ERROR, the errorClass style will not be applied to the message.

Screen Shot 2017-12-26 at 23.11.58.png

PasswordValidator.java


<abridgement>
/**Custom validator for password entry screen. */
@FacesValidator(value = "passwordValidator")
public class PasswordValidator implements Validator {
    private static final String KINSHI = "password";

    @Override
    public void validate(FacesContext context, UIComponent component, Object value)
            throws ValidatorException {
        String inputedValue = (String) value;
        if (inputedValue.equals(KINSHI)) {
            FacesMessage errorMessage = new FacesMessage(KINSHI + "Cannot be used.");
            throw new ValidatorException(errorMessage);
        }
    }
}

State seen with browser development tools


<label>Please enter the password.</label>
<br>
<input id="j_idt6:password" type="password" name="j_idt6:password" value="">
"password cannot be used.
  "
<br>
<label>Already for confirmation&nbsp;&nbsp;Please enter it once.</label>
<br>
<input id="j_idt6:rePassword" type="password" name="j_idt6:rePassword" value="">
<span class="error">Validation error:Requires a value.</span>

Setting SEVERITY_ERROR applied the errorClass style.

Screen Shot 2017-12-26 at 23.19.55.png

PasswordValidator.java


<abridgement:Others are the same as the above code>
            FacesMessage errorMessage = new FacesMessage(KINSHI + "Cannot be used.");
            errorMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(errorMessage);
<abridgement:Others are the same as the above code>

State seen with browser development tools


<label>Please enter the password.</label>
<br>
<input id="j_idt6:password" type="password" name="j_idt6:password" value="">
<span class="error">password cannot be used.</span>
<br>
<label>Already for confirmation&nbsp;&nbsp;Please enter it once.</label>
<br>
<input id="j_idt6:rePassword" type="password" name="j_idt6:rePassword" value="">
<span class="error">Validation error:Requires a value.</span>

What helped me ...

errorClass attribute: Specifies the CSS class for error messages image.png

-[Display errors / information / warnings on JSF pages --Apache MyFaces --Apache Software Foundation](http://oss.infoscience.co.jp/myfaces/cwiki.apache.org/confluence/display/MYFACES/Displaying+ Errors +-+ Infos +-+ Warnings + in + JSF + pages.html) -Sample custom validator by lab. JSF2

Recommended Posts

When displaying a message with a JSF custom validator, the errorClass of h: message is not applied unless SEVERITY_ERROR is set in the message.
How to set when "The constructor Empty () is not visible" occurs in junit
Cause of is not visible when calling a method of another class in java
When the project is not displayed in eclipse
Summary of how to use the proxy set in IE when connecting with Java
When I run docker-compose exec in crontab, I get "the input device is not a TTY"
Display a balloon message in BarButtonItem of NavigationBar with a size according to the amount of text.
[Rails] What to do when the view collapses when a message is displayed with the errors method
The idea of cutting off when the error is not resolved
[Rails] When the layout change of devise is not reflected
Find the number of days in a month with Kotlin
[Spring Data JPA] Custom ID is assigned in a unique sequence at the time of registration.
Separate one line of csv with a single-byte comma that is not enclosed in double quotes.
What to do when javax.el.ELException: Not a Valid Method Expression: appears when the JSF screen is displayed