0

当我单击一个按钮时,我会弹出一个对话框,该表单用于创建一个只有一个字段的新学校记录。我已经向这个唯一的输入字段添加了所需的验证,一切正常,但我唯一不知道该怎么做的是当我点击提交时,无论表单是否验证,对话框都会通过以下代码关闭:

 initDataTable();
    // Dismiss the dialog
    $('#dialogNewSchool').dialog('close');

在对话框关闭之前,我应该进行表单验证检查(类似于验证类的返回),我如何在 javascript 中做到这一点?我正在使用 JSF 2.1 实现。

顺便说一句,我不想​​使用客户端验证,因为该字段不仅需要验证。

谢谢。;)

4

1 回答 1

0

If I got you right you want to keep dialog open in case of validation errors?

Than you can add the following code somewhere in your page

<h:panelGroup id="global_flag_validation_failed_render">
    <h:outputText id="global_flag_validation_failed" value="true" rendered="#{facesContext.validationFailed}"/>
</h:panelGroup>

And upon clicking your submit button render the above block like this:

<f:ajax onevent="closeMyDialogIfAllOk" render="@form global_flag_validation_failed_render"></f:ajax>

and in your js code

function closeMyDialogIfAllOk(data){
    if(data.status === 'success') {
        if($("#global_flag_validation_failed").length === 0){
             $("#dialog_id").dialog("close");
        }
    }
}

For more info take a look over here: How to find indication of a Validation error (required=“true”) while doing ajax command

于 2013-09-23T06:16:29.667 回答