0

I've the below input with required="true":

<p:inputText value="#{bean.value}" required="true">
    <p:ajax event="change" listener="#{bean.change()}" />
</p:inputText>

When user changes the value, the listener is fired and I can access the changed value. When user empties the field, the listener is not fired and the empty value is not updating in my bean. I gather that this is caused by requried="true". I would like to update my bean with empty value and fire the listener anyway when the user empties the field. How can I achieve this?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user3691501
  • 53
  • 1
  • 1
  • 6
  • It is purposeless. Any action(Listener) is not invoked, when any conversion/validation violation occurs. – Tiny Jun 01 '15 at 10:34

2 Answers2

3

You can just use expression language (EL) in the required attribute too. You can then just check if the main submit button of the form has been pressed. Imagine that your form has a "Save" button like below,

<p:inputText ... required="true">
    <p:ajax ... />
</p:inputText>
...
<p:commandButton ... action="#{bean.save}" />

Then you can let the required attribute evaluate true only if the button is invoked. You can achieve that by referencing the component via binding and checking if its client ID is present in the HTTP request parameter map:

<p:inputText ... required="#{not empty param[save.clientId]}">
    <p:ajax ... />
</p:inputText>
...
<p:commandButton binding="#{save}" ... action="#{bean.save}" />

Note that #{save} is as-is and may absolutely not be bound to a backing bean, and that the variable name must be unique in the current view and EL scope.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for your answer and editing the question, i have did the same thing what you said. even though the ajax listner is not calling when i am trying to make the field empty , plz help me in this. – user3691501 Jun 01 '15 at 10:13
  • Works fine for me. Which JSF impl/version are you using? – BalusC Jun 01 '15 at 20:02
  • JSF version is 2.1 and prime faces 5.0 – user3691501 Jun 02 '15 at 07:19
  • duplicate? http://stackoverflow.com/questions/8370675/how-to-let-validation-depend-on-the-pressed-button – Kukeltje Jun 02 '15 at 13:56
  • @Kukeltje: I considered that, but as the question is asked from a different perspective I imagined that no one could understand how the answer is applicable. – BalusC Jun 02 '15 at 14:09
  • lucky me... Now I'm sure I'm somebody and not no one ;-) – Kukeltje Jun 02 '15 at 15:13
0

The issue is that if the user clears the required input field then 'required' validator throws an exception and bean setter will not be called. When the form is reloaded then cleared value will show up again from the bean. Here is my workaround:

public String getSomething() {
    return isFormValueEmpty("form:something") ? null : this.something;
}

private Boolean isFormValueEmpty(String formFieldName) {
    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    String formValue = ec.getRequestParameterMap().get(formFieldName);
    logger.debug("Check if form value is empty: [{}] [{}]", formFieldName, formValue);
    return StringUtils.isEmpty(formValue);
}