1

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 e.g. 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.

Sneek
  • 106
  • 5
  • why not use a 'temporary/working' model that on submit is copied to the real model? I use this pattern in many occasions and it works nicely for us – Kukeltje Jun 23 '15 at 21:46
  • Yeah, that was my approach too. But I first wanted to check if there is an smarter solution. – Sneek Jun 24 '15 at 08:18

1 Answers1

1

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 e.g. 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. I.e. 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?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Ah currently my bean that holds the Model is SessionScoped. I think if I change to ViewScoped it will work also like I want it. If I change to ViewScoped I can validate the input every time the user inputs a sign into the textfield. This will may update the model too, but that doesn't matter because the next time the view will be displayed the bean will be recreated (because its ViewScoped?!). Am I right? – Sneek Jun 24 '15 at 09:02
  • Yes, but make sure it is not in any commit since it will stikl end up in the db – Kukeltje Jun 24 '15 at 10:04
  • Not sure what you're implying with "?!", but just try and observe yourself. It's also very elaborate explained in the last link in above post. I suggest clicking that link if you want to learn how to choose the right bean scope. – BalusC Jun 24 '15 at 10:19