0

I need a listener function to be able to detect when a user clears an inputText field. When the text field has a new string entered into it, the backing bean value is immediately updated and then the listener function is called, however, by debugging I have noticed that when a string is cleared the backing bean variable isn't set to null till after the listener has returned. As my listener function relies upon a (backingBeanvariable == null) test this is causing errors in my functionality. Is there a way to get around this?

My xhtml:

<p:column>
    <p:inputText id="inputText" value="#{backingBean.variable}"
        title="someTitle">
        <p:ajax resetValues="true" listener="#{backingBean.onChange()}" />
    </p:inputText>
</p:column>

Backing Bean listener Function:

public void onUiiTypeChange()
{
    if (variable == null)
    {
        // some logic
    }
}

EDIT: The variable may get set to an empty string reather than null when a field is cleared. Either way, this doesn't occur until after the listener function has returned. i.e. if the field contained "asdf" before it was cleared the variable will still be set to "asdf" when the listener function is called.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Samuel Jones
  • 105
  • 10
  • FYI, your example is working correctly in my setup, I cannot reproduce. There seems to be an inconsistency, as you said you saw by debugging that the field was updated to null after the listener was called, but then you correctly note that the variable may have been set to empty string (that is the default behavior). So its possible that more thorough debugging will tell you what's going on. – kingdc Aug 03 '17 at 21:58
  • Please add PF and JSF versions to the question as requested in http://www.stackoverflow.com/tags/jsf/info – Kukeltje Aug 04 '17 at 06:59

2 Answers2

1

To achieve that your p:ajax tag should have an event that listens when a user interacts with your input field. This can be done by setting the event attribute of p:ajax tag to a listener value. To know these accepted values of the event attribute,I will point you to this answered question.

your facelets :

<p:column>
    <p:inputText id="inputText" value="#{backingBean.variable}"
        title="someTitle">
        <!--added attribute event-->
        <p:ajax resetValues="true" listener="#{backingBean.onChange()}" event="onblur"/>
    </p:inputText>
</p:column>

Now in your bean method which accepts an AjaxBehaviorEvent as a parameter and returns void you put your logic there : eg

public void onChange(AjaxBehaviorEvent event){

  //there are other ways to do this
  String[] array = yourValue.split(" ");
  //do something if array length satisfies your condition
  //etc
}

the most important thing is to note that, with JSF EMPTY STRING is not equivalent to NULL STRING and this comes as a default setting. To trump that you need to categorically indicate that in your web.xml file with

<context-param>
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
    <param-value>true</param-value>
</context-param>

Further more your version of JSF and server plays a role. reference here

basamoahjnr
  • 145
  • 11
0

I'm not sure, but isn't the variable an empty String after the reset of the user. This way you have to check variable.equals("").

T.Semmler
  • 209
  • 1
  • 12
  • You might be right that it gets set to and empty string rather than null but either way it isn't set until after the listener function has returned. – Samuel Jones Aug 03 '17 at 13:47