简体   繁体   中英

JSF Validation via AJAX without updating the model

I've did a page via JSF where the user can enter some values in a form. If the user is fine with the input, he can click a submit-button which updates the Model with the new values.

What I'm trying to achieve is: I want that the validation of the input is triggered every time, the user enters a sign into the input field. But at this time, the model should NOT be updated. The model should only be updated, if the user clicks the submit-button. I want this behaviour for a better userexperience. The user should have the ability eg to press the Back-Button in the browser and his changes are not attached to the model. Also I want the user to see at inputtime, if he enters some bullshit.

Currently my JSF-File looks like this:

    <h:inputText
        required="true"
        requiredMessage="Please enter a value."
        id="input_value" value="#{myBean.myValue}"
        styleClass="input"
        validatorMessage="Please enter a value." >
        <f:ajax 
            event="keyup"
            execute="input_value"
            render="input_value"/>
    </h:inputText>

This triggers the validation everytime the user enters a sign into the input field. But it also updates the model. And thats not what I want.

This is not possible. At least not without hacking in the JSF impl.

Fortunately you mentioned the X of your XY-problem so this can be reasonably answered:

I want this behaviour for a better userexperience. The user should have the ability eg to press the Back-Button in the browser and his changes are not attached to the model

To solve that, just instruct the browser to never cache dynamic pages. Detail can be found in this Q&A: Avoid back button on JSF web application . You also need to make sure that you choose the right bean scope for the data it holds. Ie do not put view scoped data in a session scoped bean. Those form beans must be at most view scoped. See also How to choose the right bean scope?

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