简体   繁体   English

模态对话框验证ASP.NET/bootstrap

[英]Modal Dialog Validation ASP.NET/bootstrap

I've been trying to validate some control in a modal dialog for days now, and despite all the examples and other posts here on SO I can't seem to get it working... 几天来,我一直在尝试验证模式对话框中的某些控件,尽管这里有所有示例和其他文章,但我似乎无法正常工作...

In my webpage I have a button that opens a modal dialog. 在我的网页中,我有一个打开模式对话框的按钮。 That modal dialog has three required input boxes: one for text and two for positive numeric values. 该模式对话框具有三个必需的输入框:一个用于文本,两个用于正数值。 I want to validate the inputs when the user clicks save using the fancy bootstrap feedback scheme like these examples: 我想使用类似以下示例的花式引导程序反馈方案来验证用户单击保存时的输入:

http://formvalidation.io/examples/modal/ http://formvalidation.io/examples/modal/

http://1000hz.github.io/bootstrap-validator/ http://1000hz.github.io/bootstrap-validator/

If the input is valid then I'll take the values and process accordingly. 如果输入有效,那么我将获取值并进行相应处理。 I haven't gotten this far though. 我还没有走这么远。 The modal does open currently. 该模式当前不会打开。

I know these examples use forms, but since I'm using a master page, a nested form in my content page isn't allowed. 我知道这些示例使用表格,但是由于我使用的是母版页,因此不允许在内容页面中使用嵌套表格。 So how can I validate the input and apply the feedback style to the invalid controls when the user clicks save? 那么,当用户单击“保存”时,如何验证输入并将反馈样式应用于无效控件?

<!-- Button to trigger modal -->
<button type="button" id="btnOpenModal" class="btn btn-info" data-toggle="modal" data-target="#myModal">Add</button>

<!-- Bootstrap Modal Dialog -->
<div class="modal fade" id="myModal" data-toggle="validator" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">Here is the modal dialog</h4>
            </div>
            <div id="loginForm" class="modal-body form-horizontal">
                <h5>Describe what to do here...</h5>
                <div class="form-inline form-group">
                    <label for="mdltxtId" class="col-sm-3 control-label">Description</label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control" id="mdltxtId" name="mdltxtId" placeholder="item description" />
                    </div>
                </div>
                <div class="form-inline form-group">
                    <label for="mdltxtWgt" class="col-sm-3 control-label">Weight (LB)</label>
                    <div class="col-sm-9">
                        <input type="text" pattern="^[0-9]{1,}" title="Positive number only" class="form-control" id="mdltxtWeight" name="mdltxtWeight" placeholder="weight in pounds" />
                    </div>
                </div>
                <div class="form-inline form-group">
                    <label for="mdltxtLength" class="col-sm-3 control-label">Length (IN)</label>
                    <div class="col-sm-9">
                        <input type="text" pattern="^[0-9]{1,}" title="Positive number only" class="form-control" id="mdltxtLength" name="mdltxtLength" placeholder="length in inches" />
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <asp:Button ID="btnSave" runat="server" Text="Save" CssClass="btn btn-primary" />
                <%--<button type="submit" class="btn btn-primary">Save changes</button>--%>
            </div>
        </div>
    </div>
</div>

I probably could change the project to not use a master page to make life easier with forms - it's not required (default Web Forms project in VS2015 sets this up automatically). 我可能可以将项目更改为不使用母版页,以简化表单的使用-这不是必需的(VS2015中的默认Web Forms项目会自动设置此设置)。

Just to add...I'm primarily a VB.NET winforms developer so I'm probably missing a lot of fundamentals on ASP.NET and javascript so go easy on me. 只是要补充...我主要是VB.NET winforms开发人员,所以我可能会缺少ASP.NET和javascript的许多基础知识,所以请放轻松。

Using the Bootstrap Validate plugin , you cannot validate input elements that are outside of a <form></form> . 使用Bootstrap Validate插件 ,您无法验证<form></form>之外的输入元素。 There is no workaround for this limitation. 没有针对此限制的解决方法。

with a Form id="Form" 带有表单id="Form"

<form id="Form">
<div class="modal-body form-horizontal">
    <h5>Describe what to do here...</h5>
    <div class="form-inline form-group">
        <label for="mdltxtId" class="col-sm-3 control-label">Description</label>
        <div class="col-sm-9">
            <input type="text" class="form-control" id="mdltxtId" name="mdltxtId" placeholder="item description" />
        </div>
    </div>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    <asp:Button ID="btnSave" runat="server" Text="Save" CssClass="btn btn-primary" />
    <%--<button type="submit" class="btn btn-primary">Save changes</button>--%>
</div>
</form>

Fiddle with Form 摆弄形式

When form id="Form" changed into a div id="Form" and now the same code cannot be validate by Bootstrap Validate plugin . 当表单id="Form"更改为div id="Form" ,现在无法通过Bootstrap Validate plugin验证相同的代码。 The plugin does nothing without a <form></form> . 如果没有<form></form> ,插件将无法执行任何操作。

<div id="Form" class="modal-body form-horizontal">
    <h5>Describe what to do here...</h5>
    <div class="form-inline form-group">
        <label for="mdltxtId" class="col-sm-3 control-label">Description</label>
        <div class="col-sm-9">
            <input type="text" class="form-control" id="mdltxtId" name="mdltxtId" placeholder="item description" />
        </div>
    </div>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    <asp:Button ID="btnSave" runat="server" Text="Save" CssClass="btn btn-primary" />
    <%--<button type="submit" class="btn btn-primary">Save changes</button>--%>
</div>

Fiddle with Div 小提琴小提琴

So as Shehary pointed out the Bootstrap Validate plugin won't work on a modal dialog that is not a form. 因此,正如Shehary指出的那样,Bootstrap Validate插件无法在非表单的模式对话框中使用。 So to emulate the same effect, I added a span tag to each input to provide the feedback text and use javascript to to the validating and add the feedback styles. 因此,为了模拟相同的效果,我向每个输入添加了一个span标签,以提供反馈文本,并使用javascript进行验证并添加反馈样式。

Here is the modal: 这是模态:

<div class="modal fade" id="myModal" data-toggle="validator" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">Here is the modal dialog</h4>
            </div>
            <div id="loginForm" class="modal-body form-horizontal">
                <h5>Describe what to do here...</h5>
                <div class="form-inline form-group">
                    <label for="mdltxtId" class="col-sm-3 control-label">Description</label>
                    <div class="col-sm-9">
                        <input type="text" pattern="^.{1,}" title="Item name required" class="form-control" id="mdltxtId" name="mdltxtId" placeholder="item description"/>                            
                        <span id="mdlIdHelper" class="help-block h6"></span>
                    </div>
                </div>
                <div class="form-inline form-group">
                    <label for="mdltxtWgt" class="col-sm-3 control-label">Weight (LB)</label>
                    <div class="col-sm-9">
                        <input type="text" pattern="^[+]?([.]\d+|\d+[.]?\d*)" title="Positive number only" class="form-control" id="mdltxtWeight" name="mdltxtWeight" placeholder="weight in pounds" />
                        <span id="mdlWgtHelper" class="help-block h6"></span>
                    </div>
                </div>
                <div class="form-inline form-group">
                    <label for="mdltxtArm" class="col-sm-3 control-label">Arm (IN)</label>
                    <div class="col-sm-9">
                        <input type="text" pattern="^[0-9]{1,}" title="Positive number only" class="form-control" id="mdltxtArm" name="mdltxtArm" placeholder="arm length in inches" />
                        <span id="mdlArmHelper" class="help-block h6"></span>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>                    
                <button type="button" class="btn btn-primary" id="btnmdlSave2" onclick="CloseModal(); return false;">Save</button>
            </div>
        </div>
    </div>
</div>

And here is the javascript: 这是javascript:

    function OpenModal() {
        //window.alert('opening modal');
        ResetModal();
        //window.alert('modal was reset');
        $('#myModal').modal('show');
        //window.alert('modal was opened');
    }

    function ResetModal() {
        //window.alert('beginning modal reset');
        var mdlId = $('#mdltxtId');
        var mdlWgt = $('#mdltxtWeight');
        var mdlArm = $('#mdltxtArm');

        // reset the item description input group
        mdlId.closest('.form-group').removeClass('has-error').removeClass('has-success');
        mdlId.val('');
        mdlIdHelper.innerHTML = "";

        // reset the item weight input group
        mdlWgt.closest('.form-group').removeClass('has-error').removeClass('has-success');
        mdlWgt.val('');
        mdlWgtHelper.innerHTML = "";

        // reset the arm length input group
        mdlArm.closest('.form-group').removeClass('has-error').removeClass('has-success');
        mdlArm.val('');
        mdlArmHelper.innerHTML = "";
        //window.alert('finished modal reset');
    }

    function ValidateModal() {
        var mdlId = $('#mdltxtId');
        var mdlWgt = $('#mdltxtWeight');
        var mdlArm = $('#mdltxtArm');
        var val = true

        // Check if the input is valid
        if (!mdlId.val()) {
            // Add errors highlight
            mdlId.closest('.form-group').removeClass('has-success').addClass('has-error');
            mdlIdHelper.innerHTML = "You must enter a description";
            val = false
        } else {
            // Add success highlight
            mdlId.closest('.form-group').removeClass('has-error').addClass('has-success');
            mdlIdHelper.innerHTML = "";
        }

        // Check if the input is valid
        if (!mdlWgt.val() || !$.isNumeric(mdlWgt.val()) || mdlWgt.val() <= 0) {
            // Add errors highlight
            mdlWgt.closest('.form-group').removeClass('has-success').addClass('has-error');
            mdlWgtHelper.innerHTML = "Item weight must be a positive numeric value";
            val = false;
        } else {
            // Add success highlight
            mdlWgt.closest('.form-group').removeClass('has-error').addClass('has-success');
            mdlWgtHelper.innerHTML = "";
        }

        // Check if the input is valid
        if (!mdlArm.val() || !$.isNumeric(mdlArm.val()) || mdlArm.val() <= 0) {
            // Add errors highlight
            mdlArm.closest('.form-group').removeClass('has-success').addClass('has-error');
            mdlArmHelper.innerHTML = "Arm length must be a positive numeric value";
            val = false;
        } else {
            // Add success highlight
            mdlArm.closest('.form-group').removeClass('has-error').addClass('has-success');
            mdlArmHelper.innerHTML = "";
        }

        // return false if there was an error in the modal dialog.  A FALSE return value will prevent a postback to the server.  Might be redundant since the button onclick also has 'return false'.
        return val;
    }

I faced the same problem, probably it's too late but here is the code I did. 我遇到了同样的问题,可能为时已晚,但这是我做的代码。 It's useful if you don't care about use ajax and json... 如果您不关心使用ajax和json,这将非常有用...

<!-- Modal -->
<div class="modal fade" id="modalOption" tabindex="-1" role="dialog" aria-labelledby="modalOptionLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="modalOptionLabel">Options</h4>
            </div>
            <div class="modal-body">
                @using (Html.BeginForm("Save", "Option", FormMethod.Post, new { id = "frmTest" }))
                {
                    Html.AntiForgeryToken();
                    <div class="row">
                        <div class="col-md-6">
                            <div class="col-md-6">
                                @Html.LabelFor(model => model.VALUE1)
                                @Html.TextBoxFor(model => model.VALUE1, String.Format("{0:#0.00}", Model.VALUE1), new { @class = "form-control input-decimal" })
                                @Html.ValidationMessageFor(model => model.VALUE1, "", new { @class = "label label-danger" })
                            </div>

                        </div>
                        <div class="col-md-6">
                            <div class="col-md-6">
                                @Html.LabelFor(model => model.VALUE2)
                                @Html.TextBoxFor(model => model.VALUE2, String.Format("{0:#0.00}", Model.VALUE2), new { @class = "form-control input-decimal" })
                                @Html.ValidationMessageFor(model => model.VALUE2, "", new { @class = "label label-danger" })
                            </div>
                        </div>
                    </div>
                }
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default btn-sm" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary btn-sm crud-save" id="SaveOption" data-target="#modalOption" data-container="#divOptionGrid" form="frmTest">Save</button>
            </div>
        </div>
    </div>
</div>


<script>
$(document).off('click', '.crud-save').on('click', '.crud-save', function (event) {
    var formName = $(this).attr("form");
    var form = $("#" + formName)
    form.removeData('validator');
    form.removeData('unobtrusiveValidation');
    $.validator.unobtrusive.parse(form);

    var isValid = $(form).validate().form();

    if (!isValid) {
        event.preventDefault();
        return false;
    }
    var formInfo = form.serialize();
    //here goes your ajax implementation
   $.ajax({
        type: form.attr('method'),
        dataType: "json",//response by using json
        url: form.attr('action'),
        data: formInfo,
        success: function (customJson) //just return a JsonResult from the controller if the process is successful
        {
            if(customJson.isDone){
                alert("Done");
                $("#modalOption").modal('hide');
            }
            else{                   
                alert(customJson.errorMessage);
            }                
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log(textStatus, errorThrown);
        }
    }
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM