验证失败应防止关闭模式对话框

ane

我正在使用Bootsfaces显示模式对话框。模态对话框中有一个字段(实际上是b:inputTextarea)和一个使用Ajax提交数据的命令按钮。我想确保用户在提交之前已在文本字段中输入了一些内容。如果该字段为空,则不应关闭模式并且不应进行提交;如果该字段为非空,则应关闭对话框,并且提交应采用Ajax样式。

由于Bootsfaces 1.4.2问题,我无法使用b:commandAjax-part。我必须使用符合标准的方式h:commandButtonf:ajax

我正在尝试解决这样的问题:

<b:form>
    <!--  section that gets updated by ajax-request -->
    <b:panel id="updateSection">
        <h:outputText value="#{testBean.value1}" />
        <b:button value="open dialog" onclick="$('.pseudoClass').modal()" />
    </b:panel>
    <!-- modal dialog -->
    <b:modal title="model" styleClass="pseudoClass" closable="false" closeOnEscape="true">
        <b:panel id="modalOutput"><h:inputText value="#{testBean.value1}" /></b:panel>
        <f:facet name="footer">
            <!-- cancel button -->
            <b:button largeScreen="half" value="cancel" dismiss="modal" onclick="return false;" />
            <!-- submit button ajax-style -->
            <h:commandButton value="submit" action="#{testBean.save()}" onclick="validateModalDialog();">
                <f:ajax render="updateSection" execute="@this modalOutput" onevent="closeModalDialog" />
            </h:commandButton>
            <!-- scripts to close & validate modal dialog -->
            <h:outputScript>
                    function closeModalDialog(data) {
                        var status = data.status;
                        if (status === 'complete') { $('.pseudoClass').modal('hide'); }
                    }
                    function validateModalDialog() {
                        alert('i get called before closing dialog');
                        return false;
                    }    
            </h:outputScript>
        </f:facet>
    </b:modal>
</b:form>

validateModalDialog在关闭模式对话框之前会调用脚本函数,但是无论函数的返回值是(真还是假),对话框都会关闭。

我对JavaScript的了解非常有限。这就是我选择使用JSF和Bootsfaces的原因之一。但是我很确定有一种方法可以进行验证并防止对话框关闭。

使用标准组件时h:commandButton我如何仿真Bootsfaces功能(模式对话框中文本字段的客户端验证)f:ajax

解:

为了实现客户端验证,与我的初始代码相比,需要进行以下操作以防止Ajax请求触发和模式对话框关闭以下更改:

<h:commandButton value="submit" action="#{testBean.save()}" onclick="return validateModalDialog();">
     <f:ajax render="updateSection" execute="@this modalOutput" onevent="closeModalDialog" />
</h:commandButton>
塞拉隆

确实看起来像

<h:commandButton>
    <f:ajax onevent="function(e){if(e.status == 'complete') doSomething();}"/>
</h:commandButton>

不等于

<b:commandButton ajax="true" oncomplete="doSomething();"/>

为了h:commandButton仅在表单没有验证错误时通过javascript关闭模态对话框,您必须:

  1. 添加<b:fetchBeanInfos/>组件到窗体中建议这个帖子我最初提出的重复你的问题。还要确保它随着每个ajax请求而更新。
  2. 不检查data.status == 'complete'而是data.status == 'success'
  3. onclick="validateModalDialog();"完全删除

这是为服务器端验证而更改的代码,仅在输入不为空(required="true")时关闭对话框请注意,也testBean.save()仅对有效输入调用。

<b:form>
    <!--  section that gets updated by ajax-request -->
    <b:panel id="updateSection">
        <h:outputText value="#{testBean.value1}" />
        <b:button value="open dialog" onclick="$('.pseudoClass').modal()" />
    </b:panel>
    <!-- modal dialog -->
    <b:modal title="model" styleClass="pseudoClass" closable="false"
        closeOnEscape="true">
        <b:panel id="modalOutput">
            <h:inputText id="myInput" value="#{testBean.value1}" required="true" />
            <h:message for="myInput" />
            <b:fetchBeanInfos /> <!-- creates the validationFailed JavaScript variable 
                                  and must be within rendered components. -->
        </b:panel>
        <f:facet name="footer">
            <!-- cancel button -->
            <b:button largeScreen="half" value="cancel" dismiss="modal"
                onclick="return false;" />
            <!-- submit button ajax-style -->
            <h:commandButton value="submit" action="#{testBean.save()}">
                <f:ajax render="updateSection modalOutput"
                    execute="@this modalOutput" onevent="closeModalDialog" />
            </h:commandButton>
            <!-- scripts to close & validate modal dialog -->
            <h:outputScript>
                   function closeModalDialog(data) {
                       var status = data.status;
                       if (!validationFailed &amp;&amp; status === 'success') { $('.pseudoClass').modal('hide'); }
                   }
           </h:outputScript>
        </f:facet>
    </b:modal>
</b:form>

一点示范:

在此处输入图片说明


如果要完全阻止无效表单上的Ajax表单提交并进行客户端验证,则必须onclick根据以下内容进行修改与JS客户端验证结合使用时,不会触发JSF ajax请求

<h:commandButton value="submit" action="#{testBean.save()}" 
        onclick="if(!validateModalDialog()) return false;">
    <f:ajax render="updateSection" execute="@this modalOutput" onevent="closeModalDialog" />
</h:commandButton>

当心客户端验证不会阻止用户通过例如使用其浏览器开发工具进行游戏来提交无效数据

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章