Question

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>
Was it helpful?

Solution

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.

OTHER TIPS

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();}"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top