setStyleClass method is overriding the already defined styleClass

僤鯓⒐⒋嵵緔 提交于 2019-12-12 00:32:25

问题


I have a jsf code like this:

<h:inputText id="emailText" styleClass="emailAddress" value="{bean.emailText}"/>

I am adding the style emailAddress to use it in jquery for restricting the user from copying and pasting text in textbox like below:

jQuery(".emailAddress").bind("copy paste",function(e) {
      e.preventDefault();
});

But on click of Submit, when the text field is validated and if the text entered is in improper format, bean side, I am adding another style class to the textbox to highlight it in red color like:

((HtmlInputText) getUIComponent(context, component, "emailText")).setStyleClass("error");

Now the problem is the newly added style class is overriding the existing emailAddress class which was used for restricting copy,paste in jquery. I am using only class approach in jquery as I want to apply the restricting functionality to all the email address fields through out the application. So, please help me how to handle the situation. I mean how to avoid the overriding of the existing style class when we call setStyClass(). Thanks.


回答1:


You can try another approach :

first create method in bean which returns boolean value for text-field validation.

use that method in styleClass attribute to add class "error" when it returns false.

styleClass="emailAddress #{beanName.isvalidate eq true ? 'no-error' : 'error'}"

on submit button don't forget to update h:inputText [ use update="emailText" for submit button ]



来源:https://stackoverflow.com/questions/29163189/setstyleclass-method-is-overriding-the-already-defined-styleclass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!