Commit 03862384 by Kálmán Viktor

dashboard: handle wrong inputs for vmforms

parent 99df6972
...@@ -30,4 +30,40 @@ $(function() { ...@@ -30,4 +30,40 @@ $(function() {
}); });
return false; return false;
}); });
/* if the operation fails show the modal again */
$("body").on("click", "#op-form-send", function() {
var url = $(this).closest("form").prop("action");
$.ajax({
url: url,
headers: {"X-CSRFToken": getCookie('csrftoken')},
type: 'POST',
data: $(this).closest('form').serialize(),
success: function(data, textStatus, xhr) {
var r = $('#confirmation-modal'); r.next('div').remove(); r.remove();
if(data.redirect) {
$('a[href="#activity"]').trigger("click");
}
else {
$('body').append(data);
$('#confirmation-modal').modal('show');
$('#confirmation-modal').on('hidden.bs.modal', function() {
$('#confirmation-modal').remove();
});
}
},
error: function(xhr, textStatus, error) {
var r = $('#create-modal'); r.next('div').remove(); r.remove();
if (xhr.status == 500) {
addMessage("500 Internal Server Error", "danger");
} else {
addMessage(xhr.status + " Unknown Error", "danger");
}
}
});
return false;
});
}); });
...@@ -19,6 +19,10 @@ Do you want to do the following operation on {{obj}}: ...@@ -19,6 +19,10 @@ Do you want to do the following operation on {{obj}}:
<div class="pull-right"> <div class="pull-right">
<a class="btn btn-default" href="{{object.get_absolute_url}}" <a class="btn btn-default" href="{{object.get_absolute_url}}"
data-dismiss="modal">{% trans "Cancel" %}</a> data-dismiss="modal">{% trans "Cancel" %}</a>
<button class="btn btn-danger" type="submit">{% if op.icon %}<i class="icon-{{op.icon}}"></i> {% endif %}{{ op|capfirst }}</button> <button class="btn btn-danger" type="submit"
{% if form %}id="op-form-send"{% endif %}
>
{% if op.icon %}<i class="icon-{{op.icon}}"></i> {% endif %}{{ op|capfirst }}
</button>
</div> </div>
</form> </form>
...@@ -592,8 +592,15 @@ class FormOperationMixin(object): ...@@ -592,8 +592,15 @@ class FormOperationMixin(object):
form = self.form_class(self.request.POST) form = self.form_class(self.request.POST)
if form.is_valid(): if form.is_valid():
extra.update(form.cleaned_data) extra.update(form.cleaned_data)
return super(FormOperationMixin, self).post( resp = super(FormOperationMixin, self).post(
request, extra, *args, **kwargs) request, extra, *args, **kwargs)
if request.is_ajax():
return HttpResponse(
json.dumps({'redirect': resp.url}),
content_type="application=json"
)
else:
return resp
else: else:
return self.get(request) return self.get(request)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment