Input fields get local values even after clearing - jsf-2

I have a few input fields in a form which are submitted using Save button and there is a Clear button to clear the input fields using a clear method which clears the values using setSubmittedValue("");
There is a selectonemenu on the top with a valueChangeListener which calls a method in backing bean to add a set of extra input fields for certain value of selectonemenu.(This field has ajax)
First I enter some values(invalid) on the input fields and click Save so that the validation fails and displays error messages.
Then I click Clear to clear the input fields and they clear.
Now if I click on selectonemenu to change its value, the input fields display the invalid values that I entered.
Is there any way to get around this issue?

You've set the submitted value with an empty string instead of null. This way it will be displayed during render instead of the local value. However, on the subsequent request it becomes null again and the local value will be displayed instead.
The clear button should have called EditableValueHolder#resetValue() instead of EditableValueHolder#setSubmittedValue().

Related

JqueryUI Autocomplete - Handling no selection

There are lots of similar questions to this on SO but none of them have really provided an answer to my particular case. Or at least I can't figure it out!
I have a form with an autocomplete field which is used to populate other fields if a record exists.
I have found that some users type in a value and click to the next cell using the mouse and therefore do not make a selection from the autocomplete list. It's possible that they have typed a value with a matching record in the input field and in that case I want the form to be populated with the appropriate data. Or if it doesn't, then clear the form.
The only way I can think of to check the value exists in the database is to use the change event to make an ajax call to retrieve the data but that doesn't seem like a very elegant solution and I'd be very surprised if there isn't a better way to do this since it seems to me that this would be a very common scenario...
Is there a way to retain the autocomplete array and check it against the input value in the change event? Or how else can I do it?
What you can do is stash away a copy of the data returned in the ajax call's success callback.
You can then add a blur event handler to the autocomplete input, so it'll be called whenever the user clicks away to the next field. In the event handler, check the stashed ajax data, and if there was only a single possible match, use that to populate the input.

Having button that runs code, but doesn't submit data to (or validates against) table

I'm trying to add a button to an existing form (BankAccountTable). I want to add a button to run an outside process with the value of one of the form fields as a parameter.
The value is being read using this code:
str value = element.design().controlName("FieldName").valueStr();
However when I click the button Dynamics displays what fields must be filled in. This doesn't happen if the click method doesn't reference the form fields (i.e. info("click");).
How can I:
Read the value of a field without triggering form validation?
and/or
Have a button (or command buttton) that doesn't trigger form validation?
The second question, how to avoid validation, is easy: set the button attribute SaveRecord to No.
You should rarely need to access the control value directly. A better option is usually to access the bound field directly: table.FieldName.
If the control is not bound to a field, then change the AutoDeclation attribute to Yes and access the control directly: fieldName.text(). The methods text, realValue or selection is a better choice than valueStr.

Custom ViewHandler to override javax.faces.viewstate hidden field value

Can we change the value of the inputHidden field "javax.faces.viewState" before the page render.
For the field
The value should be changed to a different length value. Can this be done by using a custom viewHandler?
Can we achieve this by extending the class to ResponseWriter.
The field is rendered by the ResponseStateManager that you obtain from the current render kit.
If you look up its API you'll see that you can't just override the field's value. You have to replace the entire thing! Since state saving is quite complex (think about both server and client algos), I would think twice about attempting this.
An alternative is using a Servlet filter to capture the entire response. The hidden field's name is standardized and you could search and replace on it. For a postback you could use the same filter to restore the param.

JSF2 Component retains the old invalid value

We have with four/five fields (one field is select box - ajax) , attached with validators(custom validators). We have a clear button in the form.
When we select the value from selectbox and the form is submitted , in case of any validation errors ,we show validation errors .When we press clear ,we render the div with ajax call and all fields are clear and validations errors also cleared.
But....if we select the value from select box ,which was first selected then it retains the old invalid enteries in the form fields.
Any clues , why it retained the old values , is there any way to clear it ?
Without posting your XML though, it's difficult to tell what's happening. I've had a similar issue: Because the values were not cleared from the backing bean, just the page, you check the box, it pushes the values back to the page.
You could clear the values from the backing bean, or just set only part of the form to render on your f:ajax rather than the entire form.

How do you pass all the contents of a ListBoxFor?

I am currently binding an IEnumerable collection to a ListBoxFor, which works as expected, sending the currently selected values on POST. However, I need to send all the values instead (essentially any value in a given ListBoxFor I consider to be required, whether selected or not). How would I go about doing this?
(I can probably rig something up in jQuery where, on-submit, it manually selects all the elements in a box, but was wondering if there was a better way.)
If you want to continue using normal browser form serialization on submit, write a javascript function to fire right before the submission (hook into an onclick event or something) which iterates through the list box control and concatenates the desired values (perhaps comma-delimited) and places it in a hidden field. The value of that hidden field will be submitted normally and you can parse the individual values from it on the server side. It's still some manual work, but you avoid messing with GUI state (i.e. selecting all desired list box items) which I agree is something you don't want to do.

Resources