1

I have a text area inside a tab of accordion panel which is a description. I am trying to edit a description and saving it. I am validating the text area so that max character shouldn't exceed 1000 character. I am using <p:message> to display validation message. Before the actual save, a confirmation dialogue will be shown to confirm the save.

<p:messages showDetail="true" autoUpdate="true" />

<p:accordionPanel dynamic="true">
    <p:tab id="_0" title="description">
        <p:inputTextarea styleClass="max" id="editDesc1" widgetVar="txtBox" value="#{testBean.description}" 
            rows="6" cols="150" validatorMessage="#{msg.AddSystem_validationMsg5}" autoResize="false">
            <f:validateLength maximum="1000"></f:validateLength> 
        </p:inputTextarea>

        <p:commandButton value="save"  oncomplete="saveDialog.show()"/>

        <p:confirmDialog message="#{msg.EditSystem_confirmMsg1}" width="200" 
            showEffect="explode" hideEffect="explode" 
            header="Confirm" severity="alert" widgetVar="saveDialog"> 
            <p:commandButton value="#{msg.EditSystem_confirmAnswer1}"  action="#{testBean.saveEdit}" process="@this" /> 
            <p:commandButton value="#{msg.EditSystem_confirmAnswer2}" onclick="saveDialog.hide()" type="button" /> 

If an user enters more than 1000 characters and tries to save it, then the validation message appears for a short time and then the confirmation dialogue pops up, causing the validation message to disappear. How do I prevent the confirmation dialogue from popping up when there is a validation error?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
shreekanth
  • 459
  • 2
  • 12
  • 27

2 Answers2

5

You need to check in oncomplete of the save button if validation hasn't failed. PrimeFaces puts a global args object in the JavaScript scope which in turn has a boolean validationFailed property. You could make use of it:

<p:commandButton value="save" oncomplete="if (args &amp;&amp; !args.validationFailed) saveDialog.show()"/>

This way the confirm dialog will only be shown if the validation has not failed.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
-1

I think you can use javascript to validate:

<script type="text/javascript">
function test(){
// validation here
if(isValidated){saveDialog.show()}
else alert('exceed ...');
}
</script>
<p:commandButton value="save"  onclick="test()"/>
Rong Nguyen
  • 4,143
  • 5
  • 27
  • 53