1
votes

I have a form in jsf for which I have created a special validator class that checks if the field is: null, empty, valid, number etc. This class throws an exception with a message if the input is not valid. The problem is that when I write letters in the input field (the input should be an int!) I get the default validation message instead of the custom one. How can I disable the default validation message?

Here is an image with the default validation message:

enter image description here

JSF code:

<h:form>
            <h:inputText value="#{deleteEmployeeBean.employeeId}"
                id="employeeId" name="employeeId">
                <f:validator validatorId="idValidator" for="employeeId"></f:validator>
            </h:inputText>
            <h:message for="employeeId" style="color: red;"></h:message>
            <h:commandButton action="#{deleteEmployeeBean.loadEmployee}"
                value="#{content.searchButton}"></h:commandButton>
        </h:form>
1
I'm surprised it was upvoted. Please provide a minimal reproducible example. We cannot do much with an image.Jasper de Vries
This - and does the validatorMessage attribute of h:inputText help?Selaron
I updated my post. The custom validator works, but it seems that when the form expects a number, the JSF shows the default message instead the custom one from my validator.John R.
I think you run into bean validation, but a MCVE is needed to check that.Jasper de Vries
@jasperdevries: co-worker? Or two accounts?Kukeltje

1 Answers

0
votes

you can use validatorMessage for your f:validator

<h:inputText value="#{deleteEmployeeBean.employeeId}"
            id="employeeId" name="employeeId" validatorMessage="your message">
            <f:validator validatorId="idValidator" for="employeeId"></f:validator>
 </h:inputText>

or you can use this for let user insert just number

    <h:inputText value="#{deleteEmployeeBean.employeeId}"
      id="employeeId" name="employeeId" validatorMessage="your message" 
      onkeypress="if(event.which &lt; 48 || event.which &gt; 57 ) if(event.which != 8) return false;">
       <f:validator validatorId="idValidator" for="employeeId"></f:validator>
   </h:inputText>