2

I've tried this piece of code for submetting a date of birth field on a form :

<h:form>
    <h2>JSF Registration App</h2>
    <h4>Registration Form</h4>
    <table>
        <tr>
            <td>First Name:</td>
            <td>
        <h:inputText label="First Name" id="fname" value="#{mybean.firstName}" required="true" />
                <h:message for="fname" />
            </td>
        </tr>
        <tr>
            <td>
                <h:inputText value="#{userBean.dob}" id="dob" required="true" > 
                    <f:convertDateTime pattern="MM-dd-yy"/> 
                </h:inputText> (mm-dd-yy) 
                <h:message for="dob"/>
            </td>
        </tr>

    </table>

    <p>
        <h:commandButton value="clique ici pour valider" action="Register" />
    </p>

</h:form>

I got this message after leaving the field empty

(mm-dd-yy) j_idt5:dob : erreur de validation. Vous devez indiquer une valeur.

why there is the jsf automatically generated id " j_idt5:dob" within the message ?

Bardelman
  • 2,176
  • 7
  • 43
  • 70

1 Answers1

4

The input component's client ID becomes the default conversion/validation message label as long as you don't explicitly specify the label attribute of the input component like so:

<h:inputText ... label="Date of birth" />

If you do it, then the message will become:

Date of birth : erreur de validation. Vous devez indiquer une valeur.


The j_idt5 in the client ID is coming from <h:form>. If you give it a fixed ID like id="register" (and keep the label of the input component omitted), then the message becomes:

register:dob : erreur de validation. Vous devez indiquer une valeur.


If necessary, you override the whole validator message by validatorMessage attribute.

<h:inputText ... validatorMessage="Please enter date of birth." />

Please enter date of birth.


Equivalently, the conversion error message from <f:convertDateTime> is overridable by converterMessage attribute.

<h:inputText ... converterMessage="Please enter date of birth in mm-dd-yy format." />

Please enter date of birth in mm-dd-yy format.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555