Wow! Please! Do it again!: Web Dev: PrimeFaces 3.4 ResetInput component

Wednesday, August 15, 2012

Web Dev: PrimeFaces 3.4 ResetInput component

PrimeFaces 3.4 RC1 is out! One of the couple of new features introduced is the ResetInput (p:resetInput) component. It can be found in the PrimeFaces Showcase Labs page as of now, and will be in the Showcase page once 3.4 GA is released. Information on this component can be found there.

Look at the following scenario:
1. There are n number of input fields. All of them are marked as required. All of them are bound to attributes in the backing bean.
2. There is also a commandButton labelled "Reset", which is NOT a typical HTML reset button (<input type="reset"). This commandButton will invoke the backing bean to clear values that are bound by the input fields.
3. Of course, there has to be a "Submit" button.
4. Now enter values for (n - 1) fields. For example, if the form has 5 fields, enter values for 4 fields only.
5. Click "Submit". Validation error occurs because all fields are required.
6. Now click "Reset". All previously entered values are intact. Why?

This is because JSF stores the submitted values in the input components' states. Updating the values in the backing bean would not work. The ResetInput component will clear the states of all editable input components.

Prior to the availability of this feature in PrimeFaces, there's a project titled PrimeFaces Extensions with a component called pe:resetEditableValues that achieves the same result as p:resetInput.