问题描述
我有一个带有很多inputText的表单,我想要突出显示那些未填充正确数据的人.
I have a form with a lot of inputText, what I want is to highlight those who are not being filled with correct data.
我尝试使用'component.valid',但它总是返回该字段无效(即字段始终为红色).
I tried to use 'component.valid' but it always return that the field is invalid (i.e. fields are always red).
这是代码:
<h:inputText value="#{creerPersonne1.nom}" id="nom"
style="#{not nom.valid ? 'border-color:red;' : 'border-color:black;'}">
<f:validateRegex pattern="^[a-zA-Z]+$"></f:validateRegex>
</h:inputText>
这是结果:
请注意,首次加载页面时,该字段也会突出显示.
note that the field is also highlighted when the page is loaded for the first time.
推荐答案
您应使用component.valid
而不是nom.valid
.
component
是当前输入组件的隐式EL对象.并且component.valid
调用服务器端组件的isValid()
方法. id
参数不能以这种方式使用.
component
is an implicit EL object for the current input component. And component.valid
calls the isValid()
method of the server side component. The id
argument cannot be used this way.
因此,您应该按以下步骤更改代码:
So you should change your code as follows:
style="#{ component.valid ? 'border-color:black;' : 'border-color:red;'}"
(不相关,但是您最好使用样式类而不是硬编码样式.有效检查也适用于styleClass
属性).
(Not related but you should better use style classes instead of hard coded styles. The valid check works for the styleClass
attribute as well).
这篇关于发生验证错误时,在JSF中突出显示inputText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!