Parsley.JS - Get An Error List Using Callback
In my project I don't want to display HTML errors, but throw an alert() with list of errors on formSubmit. I guess this should be done somehow by using listeners:onFormSubmit callb
Solution 1:
As of version 2.8.0, you can access the error messages via fieldInstance.getErrorsMessages()
Example:
<form method="post" id="myForm">
<input type="text" name="phone" value="" class="required" data-parsley-type="integer" />
<input type="submit" value="Go">
</form>
<script type="text/javascript">
$(document).ready(function() {
$("#myForm").parsley();
window.Parsley.on('field:error', function(fieldInstance){
// get messages & alert
var arrErrorMsg = fieldInstance.getErrorsMessages();
var errorMsg = arrErrorMsg.join(';');
alert(errorMsg);
// get name of the input with error
alert(fieldInstance.$element.attr('name'));
});
});
</script>
Also take a look at the following question Display parsley errors in bootstrap tooltip
Solution 2:
To solved warning
parsley.min.js:16 Parsley's pubsub module is deprecated; use the 'on' and 'off' methods on parsley instances or window.Parsley
Try this
window.Parsley.on('field:error', function () {
// This global callback will be called for any field
// that fails validation.
console.log('Validation failed for: ',
this.$element.attr('name'));
});
Post a Comment for "Parsley.JS - Get An Error List Using Callback"