3

Is it possible to reset the value of an inputText after clicking on the commandButton in JSF? The inputText UIElement provides the method ResetValue so I tried something like this:

<h:inputText  id="measurementadd" binding="#{inputTextMeasurement}">
    <f:validateRegex pattern="[a-zA-Z ]*"/>
    <f:ajax event="keyup" render="measurementaddmessage submit" execute="@this"/>
<h:inputText>
<p:commandButton id="submit" action="#{Bean.addMeasurement(inputTextMeasurement.value)}" 
value="submit" update="dataTable measurementadd measurementaddmessage" 
disabled="#{empty inputTextMeasurement.value or facesContext.validationFailed }" >
    <f:ajax event="mouseup" execute="#{inputTextMeasurement.resetValue()}" />
</p:commandButton>  
<h:messages for="measurementadd" id="measurementaddmessage"/>  

But after clicking the Button the inputTextMeasurement doesn't reset it's value.

Does someone know a good workaround for this?

I'm searching for a solution without JS and JAVA, so a realization in JSF would be very cool.

Briefkasten
  • 1,964
  • 2
  • 25
  • 53
  • is the binding correct `binding="#{inputTextMeasurement}` and not `binding="#{inputTextMeasurement.value}` ? – Kuba Nov 14 '13 at 17:10
  • @Kuba: yes, it's definitely correct. See also http://stackoverflow.com/questions/12506679/what-is-component-binding-in-jsf-when-it-is-preferred-to-be-used/ – BalusC Nov 14 '13 at 17:18

3 Answers3

6

Your mistake is here in the execute attribute:

<f:ajax event="mouseup" execute="#{inputTextMeasurement.resetValue()}" />

The execute attribute should represent a space separated collection of client IDs to include in the process/decode of the ajax request. However, you specified a listener method there.

You need the listener attribute instead:

<f:ajax listener="#{inputTextMeasurement.resetValue()}" />

(and I omitted event as it defaults here to click which is already the right one)

Interesting detail is that the other <f:ajax> in the same piece of code used the exeucte attribute the right way.


Unrelated to the concrete problem, have you looked at <p:resetInput>? This saves an ajax listener method in the bean. Replace the whole <f:ajax> with

<p:resetInput target="measurementadd" />
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

Why dont we just use

<input type="Reset"/>

This one is works fine for me! ???

qangdev
  • 336
  • 5
  • 15
  • Because this resets the entire form to default values which is not what is been asked here. In case you need that, head to http://stackoverflow.com/q/20077117 – BalusC Feb 27 '16 at 19:29
0

I have solved my problem as below

<p:commandButton id="submit" action="#{Bean.addMeasurement(inputTextMeasurement)}">

Sending back bean UIInput component. Get and Reset value in back bean.

public void addMeasurement(UIInput    
   String msr = (String) inputTextMeasurement.getValue()
   inputTextMeasurement.resetValue();
}
efirat
  • 3,679
  • 2
  • 39
  • 43