JSF:验证失败的javascript回调 [英] JSF: Validation failed javascript callback

查看:107
本文介绍了JSF:验证失败的javascript回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模板,当组件验证失败时,我可以在div中添加一个CSS错误类,它会对浏览器产生非常好的效果。

I have a template in which I can add a CSS error class to a div when the validation of a component has failed and it renders a pretty nice effect on the browser.

现在,我不需要为组件添加css类这个对我没有帮助),而是我需要改变它周围的html的css,这很简单jQuery,但是我似乎无法找到验证失败的javascript回调,这可能吗?我也使用primefaces(如果他们提供这样的功能)。

Now, I don't need to add a css class to a component (this won't help me), but rather I need to change the css of the html that surrounds it, this is pretty simple with jQuery, however I can't seem to find a javascript callback for failed validation, is this possible? I'm also using primefaces (in case they provide such capabilities).

加价:

<div class="control-group ERROR_CLASS_GOES_HERE_IF_VALIDATION_FAILED">
   <label class="control-label">Input value:</label>
   <div class="controls">
      <h:inputText class=" inputbox" type="text" required="true" /> <!--Component that can fail -->
   </div>
</div>

如果输入文本为空,我需要包装控制组的div才能拥有额外的课程。我可以把它变成< h:panelGroup> 所以它是一个JSF组件,但我仍然不知道该怎么做。 Javascript似乎更容易,因为我能做到:

if the input text is empty, I need the div that wraps the "control group" to have an extra class. I can turn it into a <h:panelGroup> so it is a JSF component but still I wouldn't know how to do it. Javascript seems easier as I can do a:

jQuery("#ID_OF_DIV").addClass("error_class") 


推荐答案

让JSF / EL根据 FacesContext#isValidationFailed( )

Just let JSF/EL conditionally print the class based on FacesContext#isValidationFailed().

<div class="control-group #{facesContext.validationFailed ? 'error_class' : ''}">

您只需确保ajax update / render涵盖此元素。

You only need to ensure that this element is covered by ajax update/render.

另一种方法是挂钩基于任意PrimeFaces ajax的组件的 oncomplete 事件。范围内有一个 args 对象,而该对象又有一个 validationFailed 属性。例如。 < p:commandButton oncomplete> 甚至< p:ajaxStatus oncomplete>

Another way would be hooking on the oncomplete event of an arbitrary PrimeFaces ajax based component. There's an args object available in the scope which in turn has a validationFailed property. E.g. <p:commandButton oncomplete> or even <p:ajaxStatus oncomplete>.

<p:ajaxStatus ... oncomplete="if (args &amp;&amp; args.validationFailed) $('#ID_OF_DIV').addClass('error_class')">

这篇关于JSF:验证失败的javascript回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆