Parsley.js: Remove need for `addError`, `removeError`, ...

With the right set of feature, there shouldn’t be a need for addError, updateError and removeError.

Others?

About this issue

  • Original URL
  • State: open
  • Created 8 years ago
  • Comments: 18 (2 by maintainers)

Commits related to this issue

Most upvoted comments

I’m using v2.3.11 and there appears to be no addError() method on the instance: $('#login-form').parsley().addError is undefined so I was wondering how do I manually activate errors now since calling window.ParsleyUI.addError directs me to this issue.

The docs seem to be out of date and still suggest using window.ParsleyUI.addError.

My use case is that I use an API which does not have a possibility to AJAX check if the a field is valid. I only get errors after POSTing the entire form, e.g. if I submit a register form the API might respond with:

{
    "message": "VALIDATION_ERROR",
    "body": {
        "error_fields": {
            "email": {
                "failed_rule": "Unique",
                "message": "The email has already been taken."
            },
            "password": {
                "failed_rule": "Complexity",
                "message": "The password is not complex enough."
            }
        }
    }
}

Which is a great response which I can easily traverse and map the error message to the proper fields, but I can not see how that is possible with the latest Parsley version.

This is a very simple example, but I’m dealing with very complex CMS forms which can return errors for multiple fields with no way to validate all the error types client-side before submit.

I could of course display bulk server messages or render them manually in each error container but I would love using Parsley for this since I’m already using it for client side validation.

I was convinced I tried it on field instance as well, but probably missed something. Anyway if someone else finds this thread like me here’s a little help on how to do it (at least until the docs get updated).

$('[name="email"]').parsley().addError('error-1', { message: 'Message for error-1' }); // add error message
$('[name="email"]').parsley().addError('error-2', { message: 'Message for error-2' }); // add one more error message
$('[name="email"]').parsley().removeError('error-1'); // remove first message

I agree with your point that the custom validators should replace the functions addError, updateError and removeError, but with the current custom validators I am not able to implement it for all situations. For example:

  1. When trying to check whether a username/password combination is correct. You do not want to constantly show the user whether they have typed the wrong/correct password. You would need to have a custom validator that is only being triggered on form:validated.
window.Parsley.addValidator('password', {
    validateString : function (value) {
        var password = $("#password").val();
        var result = getUser();
        return (result.length!==0 && hash.verify(password,result[0].user_password) );
    },
    messages : {
        en : lang("error_passwordincorrect")
    }
});
  1. When retrieving data from a database using NodeJS. Because of the possible time delay from the database, the result is not yet returned and Parsley will show an error. Normally you would create a callback function that is being called after the result is returned.
window.Parsley.addValidator('emailName', {
  validateString: function(value, requirement) {
    var result;
    db.getOnlineQuery("getUserIdbyEmail", [value], function(rows) {
      // Callback from database
      result = rows;
    });
    return (result.length===0);
  },
  messages: {
    en: lang("error_emailnotunique")
  }
});

And maybe there are some more cases in which the custom validators cannot be used. If I need to explain things better, please let me know.

I am having another reason why I cannot use Custom Validators: My backend is not able to return Server Side Validation on a per field basis, but only for all fields. Therefore I am doing an ajax request to do the server side validation and then appending the errors to the different fields…