lapots
lapots

Reputation: 13415

close modal dialog if no errors returned

I'm using jquery form plugin.

And I want to close modal form window when no errors was returned from server. If I do like this

$(document).ready(function() {
    var options = {
            url: 'reg/registration.html',
            type: 'post',
            success: function(data) {
                removeErrors();
                var data = JSON.parse(data);
                $.each(data, function(i, item) {
                    addError("#input" + item.field, item.codes[3]);
                });
            }
        };
        $("#registrationForm").ajaxForm(options);
});
$("#registerBtn").click(function() {
    $("#registrationForm").submit();
    return true;
});

it closes modal window. If I return false it doesn't close it.

But I want to close modal window only if length of data in success function is 0. How to do it?

My modal window

<div class="modal" id="register" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4>New user registration <span class="glyphicon glyphicon-user"></span></h4>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-8 col-md-offset-2">
                            <form:form role="form" id="registrationForm" commandName="registerForm">
                            </form:form>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button id="registerBtn" class="btn btn-primary" data-dismiss="modal">Register</button>
                    <button class="btn btn-danger" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>

Upvotes: 0

Views: 292

Answers (1)

blgt
blgt

Reputation: 8215

You could conditionally close it in the callback:

        success: function(data) {
            removeErrors();
            var data = JSON.parse(data);
            if(data.length === 0) {
                $("#register").remove();//this will call close and destroy
            } else {
                $.each(data, function(i, item) {
                    addError("#input" + item.field, item.codes[3]);
                });
            }
        }

And then use this:

$("#registerBtn").click(function() {
    $("#registrationForm").submit();
    return false; // This disables the default closing behaviour
});

Upvotes: 1

Related Questions