0

i have a jsf-form with an input field and a save-button as seen in the code below. What i want to achieve is, when the save-button clicked, the input should be validated with the regex-pattern. If the validation failed, no save-confirmation-dialog should be shown. Otherwise a save-confirmation-dialog shown, and let the user to choose if to save or not.

In the code below, the dialog has always been shown, despite the conditional onclick="if(#{conditionOK}). I want no confirmation-dialog got shown, when conditionOK returns false!!! After many tries, i think the facescontext.isValidateFailed() will not be re-evalutated.

Please help :(

All what i want, is only to check, if the regex-Validator returns true. For this case, the confirmation-dialog should be shown.

My approach could be wrong. Many thank if you guys have also other solutions.

<h:form id="save_all_form">
      <p:inputTextarea rows="1" style="width:100%;resize:none"
         value="#{cusBean.saveAll}" autoResize="false"
         validatorMessage="Wrong format">
         <f:validateRegex pattern="#{msgs.pattern}" />
      </p:inputTextarea>

      <ui:param name="conditionOK" 
         value="#{facesContext.postback and !facesContext.validationFailed}"  />

      <p:commandButton value="#{msgs.button_overwrite_all}"
         onclick="if(#{conditionOK}){confirmation.show()}"/>
</h:form>
kolossus
  • 20,559
  • 3
  • 52
  • 104
Tim Long
  • 2,039
  • 1
  • 22
  • 25
  • So when the page is rendered and the user types something into the textarea which will not pass the validation, you would like to show the confirmation dialog? Please explain the process a little bit more detailed. – user1983983 Oct 11 '13 at 09:38
  • I have updated my question. Its annoying that somebody has edited the question. – Tim Long Oct 11 '13 at 10:08

2 Answers2

0

I do not think that the JSF-validation is the way to go for you. It is intended to prevent the change of model data in the case, that the validation fails.
And if you would like to make a check in JavaScript you have to update the section in HTML. JavaScript does not reevaluate the Expression, so the value when the view was rendered the first time will be used everytime.

Try the following in the xhtml:

<h:form id="save_all_form">
  <p:inputTextarea id="input" rows="1" style="width:100%;resize:none"
     value="#{cusBean.saveAll}" autoResize="false">
     <p:ajax global="false" update="input submit" partialSubmit="true"/>
  </p:inputTextarea>

  <p:commandButton id="submit" value="#{msgs.button_overwrite_all}"
     onclick="if(#{cusBean.validate(msgs.pattern)}){confirmation.show()}"/>
</h:form>

And add this method in CusBean:

public boolean validate(String pattern) {
  return getSaveAll().matches(pattern);
}

The result will be, that there is not JSF validation which takes place and the value of the textArea is submitted everytime you change it. Plus the commandButton-section is updated so the condition will be updated.

user1983983
  • 4,793
  • 2
  • 15
  • 24
  • You means, facescontext.validateFailed is a Javascript-call? It's not. As mentioned in [this post](http://stackoverflow.com/questions/6434866/defining-and-reusing-variable-in-jsf-page) ui:param will be re-evaluated everytime the value get used. – Tim Long Oct 11 '13 at 11:35
  • Thats exactly the opposite of what I said, I said that `if(#{conditionOK}){confirmation.show()}` is a JavaScript-call and as you are never updating the commandButton, `#{conditionOK}` will always be the initial value which is `true`. Expressions are only evaluated at view render time. – user1983983 Oct 11 '13 at 11:42
0

Like the other answer explained onclick event is too early to check the validation status of a JSF request(using !facesContext.validationFailed) because the request has not been submitted yet; Validation has not been run so the validation status will always be false (well, sort of) during onclick.

So what you'll want to do is carry out an ajax validation of the field (like shown in the earlier answer) and then use the primefaces args variable to check the status of the request:

<p:commandButton value="#{msgs.button_overwrite_all}" id="createReport" onclick="if(!args.validationFailed){confirmation.show();}"/>
kolossus
  • 20,559
  • 3
  • 52
  • 104