简体   繁体   中英

Trigger JSF validation using ajax after focus lost

当组件使用ajax失去焦点而不是等待手动提交表单时,如何在输入组件上触发验证?

Put a <f:ajax event="blur"> in the UIInput component which re-renders a <h:message> associated with the component in question.

<h:inputText id="foo" value="#{bean.foo}" required="true">
    <f:ajax event="blur" render="fooMessage" />
</h:inputText>
<h:message id="fooMessage" />

See also JSF 2.0 tutorial with Eclipse and Tomcat - the view and finetuning validation

Try this code:

<h:inputText value="#{bean.value}" valueChangeListener="#{bean.validateValue}">
    <f:ajax event="blur" render="valueError"/>
</h:inputText>

<h:outputText id="valueError" value="#{bean.valueErrorMessage}" style="color: red;" />

If the user changes the value in your input component you can validate it with your valueChangeListener. If the user then moves to another input component the ouputText component will be rendered. There you can display a message if the validation failed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM