简体   繁体   中英

f:ajax call is not working with f:validateRegex

To my surprise, ajax call is NOT happening after each key that I enter in my text box. ajax is getting invoked only after 14 digits are entered. Not sure how ajax is related to validateRegex tag handler in this case? also, can I say <f:validateRegex>, <f:valiadteLength> etc will perform validations always at client side (like java script/jquery)?

<h:inputText id="input" value="#{bean.input}" maxlength="16">
<f:ajax event="keyup" render="another_id" listener="{bean.method}"/>
<f:validateRegex pattern="[0-9]{14}" />
</h:inputText>

ajax call is NOT happening after each key that I enter in my text box. ajax is getting invoked only after 14 digits are entered

There's some confusion going on. The above statements are not true.

The ajax call is definitely invoked. Track the HTTP traffic in webbrowser's builtin developer toolset (press F12 in Chrome/IE9/Firebug and check the Network section). Create a HTTP filter on /* or a JSF PhaseListener and you'll see that it's been invoked on every key press (which means that the server is definitely hit every time).

You perhaps actually meant that the action listener method as specified in <f:ajax listener> is not been invoked. That is in turn indeed true. But that's fully expected behaviour. There's namely a validation error. This way JSF will skip the UPDATE_MODEL_VALUES and INVOKE_ACTION phases. So the action method is by design indeed not invoked. The business logic should namely not be performed when there's a validation error.

I'm not sure why you expected it to be invoked. Why would you execute business logic on invalid input? If you elaborate the concrete functional in more detail, then we may be able to propose the right approach for this.


Update as per the comments, just check the validity of the input component in the disabled attribute of the other components entirely in the view side by checking the outcome of UIInput#isValid() which you can get by binding the component to the view:

<h:inputText ... binding="#{input}">
    <f:ajax event="keyup" render="input2 input3 input4" />
    <f:validateRegex pattern="[0-9]{14}" />
</h:inputText>

<h:inputText id="input2" ... disabled="#{not input.valid}" />
<h:inputText id="input3" ... disabled="#{not input.valid}" />
<h:inputText id="input4" ... disabled="#{not input.valid}" />

No need for an ajax listener method.

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