在错误验证消息上获取组件ID [英] Getting the component id on the error validation message
问题描述
我已经尝试过用这段代码将出生日期字段与表单相符:
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.
为什么消息中包含jsf自动生成的ID"j_idt5:dob"?
why there is the jsf automatically generated id " j_idt5:dob" within the message ?
推荐答案
只要您未明确指定输入组件的label
属性,输入组件的客户端ID便成为默认的转换/验证消息标签. :
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:
出生日期:未确认. Vous devez和valeur的信使.
Date of birth : erreur de validation. Vous devez indiquer une valeur.
客户端ID中的j_idt5
来自<h:form>
.如果给它一个固定的ID,如id="register"
(并保留输入组件的label
),则消息将变为:
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:错误的验证. Vous devez和valeur的信使.
register:dob : erreur de validation. Vous devez indiquer une valeur.
如有必要,可以通过validatorMessage
属性覆盖整个验证器消息.
If necessary, you override the whole validator message by validatorMessage
attribute.
<h:inputText ... validatorMessage="Please enter date of birth." />
请输入出生日期.
Please enter date of birth.
等效地,来自<f:convertDateTime>
的转换错误消息可由converterMessage
属性覆盖.
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." />
请以mm-dd-yy格式输入出生日期.
Please enter date of birth in mm-dd-yy format.
这篇关于在错误验证消息上获取组件ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!