Permalink
Browse files

Show form errors in Twitter-Bootstrap style.

  • Loading branch information...
1 parent e004d5c commit 86690be1af8cbda281c34db9aceb66e20710e7a9 @vdboor vdboor committed Aug 28, 2012
@@ -1,3 +1,5 @@
from django.conf import settings
FLUENT_COMMENTS_REPLACE_ADMIN = getattr(settings, "FLUENT_COMMENTS_REPLACE_ADMIN", True)
+
+CRISPY_TEMPLATE_PACK = getattr(settings, 'CRISPY_TEMPLATE_PACK', 'bootstrap')
@@ -136,6 +136,7 @@
success: function(data) {
commentBusy = false;
removeWaitAnimation();
+ removeErrors();
if (data.success) {
var $added;
@@ -208,16 +209,24 @@
function commentFailure(data)
{
- // Show errors
- $('form.js-comments-form ul.errorlist').each(function() {
- this.parentNode.removeChild(this);
- });
-
- for (var error in data.errors) {
- $('#id_' + error).parent().before(data.errors[error])
+ // Show mew errors
+ for (var field_name in data.errors) {
+ if(field_name) {
+ var $field = $('#id_' + field_name);
+
+ // Twitter bootstrap style
+ $field.after('<span class="js-errors">' + data.errors[field_name] + '</span>');
+ $field.closest('.control-group').addClass('error');
+ }
}
}
+ function removeErrors()
+ {
+ $('form.js-comments-form .js-errors').remove();
+ $('form.js-comments-form .control-group.error').removeClass('error');
+ }
+
function getCommentsDiv()
{
var $comments = $("#comments");
View
@@ -10,6 +10,7 @@
from django.utils.html import escape
from django.views.decorators.csrf import csrf_protect
from django.views.decorators.http import require_POST
+from fluent_comments import appsettings
@csrf_protect
@@ -121,8 +122,9 @@ def _ajax_result(request, form, action, comment=None):
json_errors = {}
if form.errors:
- for field in form.errors:
- json_errors.update({field: str(form.errors[field])})
+ for field_name in form.errors:
+ field = form[field_name]
+ json_errors.update({field_name: _render_errors(field)})
success = False
comment_html = None
@@ -143,3 +145,14 @@ def _ajax_result(request, form, action, comment=None):
})
return HttpResponse(json_response, mimetype="application/json")
+
+
+def _render_errors(field):
+ """
+ Render form errors in crispy-forms style.
+ """
+ template = '{0}/layout/field_errors.html'.format(appsettings.CRISPY_TEMPLATE_PACK)
+ return render_to_string(template, {
+ 'field': field,
+ 'form_show_errors': True,
+ })

0 comments on commit 86690be

Please sign in to comment.