问题
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
callback, however none of passed variables does not contain "clean" version of errors. Parsley documentation also lacks of this feature.
回答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
回答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'));
});
来源:https://stackoverflow.com/questions/18120734/parsley-js-get-an-error-list-using-callback