Tiny
Tiny

Reputation: 27909

Why does p:resetInput require properties of a managed bean to be set to null first after the form is submitted?

In a view scoped managed bean, I'm using <p:resetInput> to clear the values held by the properties in the corresponding manged bean like,

<p:commandButton value="Reset" update="panel" process="@this">
    <p:resetInput target="panel" />
</p:commandButton>

This works fine.


I have a submit button <p:commandButton> which when pressed causes the submitted values to be inserted into the database, if validation succeeds.

<p:remoteCommand name="updateTable" update="dataTable"/>

<p:panel id="panel" header="New">
    <p:outputLabel for="property1" value="property1"/>

    <p:inputText id="property1" value="#{bean.property1}" required="true">
        <f:validateLength minimum="2" maximum="100"/>
    </p:inputText>

    <p:message for="property1" showSummary="false"/>

    <p:commandButton id="btnSubmit"
                     update="panel messages"
                     oncomplete="if(!args.validationFailed) {updateTable();}"
                     actionListener="#{bean.insert}"
                     value="Save"/>

    <p:commandButton value="Reset" update="panel" process="@this">
        <p:resetInput target="panel" />
    </p:commandButton>
</p:panel>

The command button invokes the insert() method in the managed bean which is defined as follows.

public void insert() {
    if (service.insert(property1)) {
        //...Popup a success message.
        reset(); //Invoke the following private method.
    } else {
        //...Show the cause of the failure.
    }
}

private void reset() {
    property1 = null; //Set this property of type String to null.
}

If this reset() method is omitted, then <p:inputText> will not be cleared as obvious but then if I press the reset button as shown in XHTML, <p:inputText> should be cleared but it doesn't.

The showcase example demonstrates exactly the same thing. Therefore, this behaviour appears to be documented but I don't understand why doesn't <p:resetInut> clear the value of property1, if the reset() method is omitted, in this case?

Upvotes: 3

Views: 7217

Answers (1)

BalusC
BalusC

Reputation: 1109865

The <p:resetInput> does not clear the model values as you incorrectly seemed to expect. It just clears the input component's state which may be dirty after a validation error.

The concrete problem it is trying to solve is in detail described in this answer: How can I populate a text field using PrimeFaces AJAX after validation errors occur?

This is the best understood by the following use case:

  1. You have a single view with a single datatable and a single dialog which displays the currently selected record for editing.
  2. You open the dialog and submits its form with invalid values. The input components are marked invalid and highlighted red.
  3. You close the dialog without fixing the errors.
  4. Then you select same or another row for editing. The dialog shows up, but the input components are still marked invalid and highlighted red and show the old submitted value -if any- because it's still the same view state you're working with.

Putting <p:resetInput> with target on dialog's form in the "open dialog" button fixes it.

I'm not sure if your particular case is the right use case for which <p:resetInput> is the right solution. Your code is not complete and you didn't state the concrete functional requirement behind this code anywhere, but as far as I see, there are no multiple inputs/forms which need to update each other. I believe that your case would still work even if you remove <p:resetInput>. So it would be totally superflous in your context and you could just get away with clearing the model (or.. just with refreshing the page by a synchronous GET button which implicitly recreates the view).

See also:

Upvotes: 8

Related Questions