I’m struggling to get my bean to update with the new page values. I have two submit buttons on my page and I toggle which one displays based on a Boolean value for what mode my page is in. When my page is in update only (no validation) I show the submit button that has immediate=”true”. When the page is in process mode (validate) I show the submit button that does not have immediate=”true”.
The problem I’m running into is when I am in update mode (no validation) the values in the input fields are not being set in the bean. All I want to do when in this mode is save the page as is and exit. No validation is needed because the information on that page is not ready to process or “really use” if you will. That said, if I have my page in process mode (validate) then everything works as intended. Values are submitted and saved.
I’m not posting any code yet as there is nothing special about what I’m trying to do. I simply have a value binding that points to simple getter / setter. My bean is in #ViewScope.
I’ve tried using the examples by BalusC in his excellent blogspot post: debug-jsf-lifecycle.
Putting immediate=”true” on the input fields has no affect when clicking on the submit button with immediate="true". All and all though, the way I understand it is immediate=”true” on the UICommand is what tells the application to skip validation or not. Putting it on the input fields simply makes validation happen sooner. Am I missing something?
Any ideas? Any and all help with this is most appreciated!
App specifics:
JSF 2.0.3
Tomcat 6.0.14
The immediate="true" is not intented to disable validation. It's intented to either prioritize validation or to skip processing of the input altogether. See also the summary at the bottom of the article.
You need to disable validation by setting required="false", or <f:validator disabled="true">. Here's an example which assumes that you've a boolean process property which represents the form's state:
<h:inputText value="#{bean.value1}" required="#{bean.process}" />
<h:inputText value="#{bean.value2}" required="#{bean.process}">
<f:validator validatorId="someValidatorId" disabled="#{!bean.process}" />
</h:inputText>
...
This way the fields aren't required and won't be validated when process evaluates false.
Related
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.
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?
I have a simple form for creating a record - it has some fields which are required and two buttons, one for submit and one for cancel. The problem is that cancel button is not working (it actually always reloads the view for creating record), unless all required fields are entered.
Field looks like this
<h:inputText id="name"
value="#{userController.User.name}"
required="true"
requiredMessage="This field is required"
maxlength="11" tabindex="22" />
In spring webflow definition i added validation="false", also tried binding="false", but it didn't help, although I'm not sure if it is relevant, as I have problem with jsf validation, not spring webflow validation.
Also to add that I'm using Richfaces 4, it might be useful information.
Thanks in advance for your answers.
Stefan
Your problem is not related to Spring Webflows or Richfaces, but to the standard JSF lifecycle that handles every request to the server.
This cycle consists of several phases (google "jsf lifecycle" for plenty of illustrations and explanations). One of them is processing all validators. If validation fails, the cycle will be aborted and a response (current page + messages) is sent. The method invocation phase, which would process your cancel button, will not be reached.
When adding immediate=true to the button it will move the processing of this button to the process validations phase, hence it will be called before processing the validation failure events (and thus the re-rendering of the page).
Try adding immediate="true" to your Cancel button XHTML
Should immediate="true" never be used when dealing with an AJAXified JSF 2.0 component?
The example might be:
If I want to implement a "Cancel" button on JSF 2.0 page where if the user hits "Cancel", no validations should run, should I set immediate="true" on the component if it is an ajaxified component or should specify that no components on the form should be processed? If so, what is the way to implement this functionality using the AJAXified features of the component and not the "old way" of using immediate="true"?
Indeed, the purpose of using immediate="true" on a cancel button has become useless since <f:ajax> which you can just set to execute="#this" (which is the default already) to skip the processing of all input components in the same form.
You may only run into problems when the form was already been submitted beforehand but failed due to a conversion/validation failure. If you hit the cancel button by ajax and render the form thereafter, the inputs would still be marked invalid and any changes in the model value are not reflected (you're probably doing a entity = new Entity(); or something in cancel action method to clear out the old values of the form). This problem does not happen when using a synchronous (non-ajax) request with immediate="true". This issue has actually nothing to do with immediate="true", but with the JSF lifecycle of ajax requests. You basically need to reset the invalidated state of the involved input components by calling EditableValueHolder#resetValue() on the components which are not included in the ajax execute, but are included in the ajax render. OmniFaces has a ResetInputAjaxActionListener for exactly this purpose.
Using JSF 2.0 validation, is there a way to have a summary message on submit if there is an error (as well as keeping individual form element error messages)?
I have a large form so the problem I am encountering is that when the user clicks "SUBMIT" it will display the error information next to each individual form element but the user cannot easily tell that errors exist on the page.
Along the lines, what is the common or recommended practice?
How about this?
<h:outputText value="There are messages" rendered="#{not empty facesContext.messageList}" />
Alternatively you can also just use JavaScript to put focus on the first invalid input element. That's a more common practice.
I generally use
<h:messages>
at the top of the page along with label attribute of components to describe each component in error message.
link here
and here might be useful
Here is the issue, JSF validation keeps flipping a field back to the last known value.
We are editing a page where the backing bean already has values.
(frequency = "weekly")
And we are required to show the default value of "please select.." even though that value will not pass validation (yes, I just want to leave the user on the page with the error message).
Is there any way to allow the user to choose "Select.." and not reset it to the last good value?
The user wont be allowed to save obviously, but we want to leave their invalid value selected.
User adds new object, selecting proper value from drop down
User saves successfully.
User Clicks "edit" and displays object with known value ("weekly")
User changes "weekly" to "select"
User clicks save
Validation message is shown (good) but frequency goes back to last value "weekly" (bad, I need it to stay on "Please select.." and let the user fix the drop down manually.
immediate="true" does not work on inputComponents, only commandComponents.
I recognize this, I've reported this more than one year ago as JSF issue 1299. This is still not resolved since it has a low priority. This is not specific to all UIInput components, but to MenuRenderer which is responsible for rendering the HTML <select> elements. All other HTML input elements behave as you would expect, the submitted value will be redisplayed (well, which is actually nothing as well).
Since you're already on JSF 2.0, I suggest you to solve this with a little help of f:ajax so that the dropdown won't be re-rendered and thus keeps its selection.
<h:selectOneMenu id="frequency" value="#{bean.frequency}" required="true">
<f:selectItems value="#{bean.frequencies}" />
<f:ajax render="frequencyMessage" />
</h:selectOneMenu>
<h:message id="frequencyMessage" for="frequency" />
The additional benefit is that the enduser has instant feedback and this is better for user experience.