发生验证错误时突出显示 JSF 中的 inputText [英] Highlight an inputText in JSF when a validation error occurs
问题描述
我有一个包含很多 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
.
You should use component.valid
instead of 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!