Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
39 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,44 @@ | ||
/* | ||
* Parsley.js - django admin helper | ||
*/ | ||
* Django admin helper for Parsley.js. | ||
* | ||
* This calls `parsley` for all forms, and sets up event listeners | ||
* to handle success and errors on field validation. | ||
* | ||
* It supports [django-grappelli](https://github.com/sehmaschine/django-grappelli). | ||
*/ | ||
|
||
!function ($) { | ||
(function ($) { | ||
'use strict'; | ||
|
||
$( window ).on( 'load', function () { | ||
$( 'form' ).each( function () { | ||
var is_grappelli = $('#grp-container').length; | ||
var form_selector = is_grappelli ? '.grp-change-form form' : '.change-form form'; | ||
var row_selector = is_grappelli ? '.grp-row' : '.form-row'; | ||
|
||
$( form_selector ).each( function () { | ||
$( this ).parsley({ | ||
errorsWrapper: '<ul class="errorlist"></ul>', | ||
errorsContainer: function (field) { | ||
if (is_grappelli) { | ||
// Grappelli appends the errors to the field (div). | ||
// ".grp-tr .grp-td" handles inline forms. | ||
return $("<div />").appendTo( | ||
field.$element.closest(".grp-tr .grp-td, div") | ||
); | ||
} | ||
return $("<div />").prependTo( | ||
// "tr.form-row td" handles inline forms. | ||
field.$element.closest("tr.form-row td, div.form-row") | ||
); | ||
} | ||
}).subscribe('parsley:field:error', function (field) { | ||
var container = field.$element.closest(row_selector); | ||
console.log(container); | ||
container.addClass("errors grp-errors"); | ||
}).subscribe('parsley:field:success', function (field) { | ||
var container = field.$element.closest(row_selector); | ||
container.removeClass("errors grp-errors"); | ||
}); | ||
} ); | ||
} ); | ||
}(window.jQuery || window.Zepto); | ||
})(window.jQuery || window.Zepto); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters