0

I am using JSF with Primefaces 5.3 and having trouble with enabling/disabling a commandButton.

First of all, I have this input:

<h:inputText id="series" value="#{bean.series}"
    required="false" converterMessage="Please enter three digits" >
    <f:validateRegex pattern="[0-9]{3}" />
    <p:ajax event="keyup" update="download"/>
    </h:inputText>

In the backing bean, this field is an Integer

private Integer series;

And my goal is to enable/disable a 'download' button, whenever the input is not empty. I do not care if it is not valid, but only if it contains no characters.

<h:commandButton id="download" value="Download"
action="#{bean.download}" 
disabled="#{empty bean.series}"/>

The update event is triggered on keyup, but because of the validation, the attribute 'series' in the backing bean is always null.

Could you please help me with a solution? Thanks

Oana Colacel
  • 1
  • 1
  • 3
  • ID for button doesn't match in "update" – 01000001 Sep 04 '18 at 16:17
  • sorry, just an edit mistake. I will edit the question, the id is correct. – Oana Colacel Sep 04 '18 at 16:19
  • Try adding process="series" to p:ajax – 01000001 Sep 04 '18 at 16:24
  • tried it, doesn't work. The getter for series is called, but is still null . Basically I enter 'abcd' but the regex does not match the input, so it does not set the value of series in the bean. If the series attribute remains null in the backing bean, my button is not enabled. – Oana Colacel Sep 04 '18 at 16:32
  • Correct and there is no reason for your button to be enabled since you cannot submit it anyway due to the field being invalid. But if you actually want this, use https://stackoverflow.com/questions/17926245/disable-enable-hcommandbutton-on-validation-fails-is-ok in a creative way and you can get your answer – Kukeltje Sep 04 '18 at 17:15

1 Answers1

0

First let me state that the requirement is illogical, since if the value is invalid, you cannot (at least not without other changes in your code) submit the form via the button.

But if you really want this, use what is done in Disable/Enable h:commandButton on validation fails/ is ok

<h:inputText id="series" value="#{bean.series}"
    required="false" converterMessage="Please enter three digits" binding="#{series}">
   <f:validateRegex pattern="[0-9]{3}" />
   <p:ajax event="keyup" update="download"/>
</h:inputText>

<h:commandButton id="download" value="Download" 
   action="#{bean.download}"  
   disabled="#{ ... EL using 'empty bean.series' and 'series.valid' ..." />

The EL has to be defined by you since you know best which combinations of empty/not empty and valid/not valid should make the button disabled

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
  • Thanks for the reply. The requirement is when you land on the page and the input is empty, the button should be disabled. Once you type something in that input, even though it is not valid digits but letters for example, the button should appear enabled. With this implementation, the button remains disabled, even though I write 'abc' or '111' when disabled attributes looks like this: disabled="#{series.valid and empty bean.series}" – Oana Colacel Sep 04 '18 at 18:10
  • I understand the requirement, but it is still strange. What if you add ` before the commandButton and update that too? Just to check if the EL is right – Kukeltje Sep 04 '18 at 18:33