behobu
behobu

Reputation: 21

custom parsley form validation not working

I have a form with one field that needs a custom validator for which I'm trying to use Parsley v2.2.0-rc1 and jQuery 2.1.4. I have confirmed that both jQuery and Parsley are loading (in that order) using my browser's developer tools to ensure I get HTTP 200 for all files. Here's what I'm trying to do:

<html>
  <form id="stuff" data-parsley-validate>
    <textarea id="stuffList" data-parsley-stuffList></textarea>
    <input type="submit">Submit</input>
  </form>
  <script type="text/javascript" src="jquery-2.1.4.min.js"></script>
  <script type="text/javascript" src="parsley.min.js"></script>
  <script type="text/javascript" src="mycode.js"></script>
</html>

mycode.js:
  jQuery(document).ready(function($) {
    window.Parsley.addValidator('stuffList', {
      requirementType: 'string',
      validateString: function(value) {
                        console.log('Starting validation');
                        if (something) {
                          return true;
                        } else {
                          return false;
                        }
                      },
      messages: {en: 'That stuff is not valid.'},
    });
  });

When I click submit, I don't even get the console.log message to let me know that the validation function was even called. What am I doing wrong?

P.S. I've tried it both with the parsley-data-validate in the form tag and not, and with some value set for data-parsley-stuffList and not. None of those variations cause the validator to execute.

Upvotes: 2

Views: 2773

Answers (2)

Phil C
Phil C

Reputation: 980

Add your validator to ParsleyValidator object likes so

window.ParsleyValidator.addValidator('stufflist', function(value, requirements) {
    console.log('Starting validation');
    if (value == 'something') {
        return true;
    } else {
        return false;
    }
}, 34);    

add the custom message

window.ParsleyConfig.i18n.en.stufflist='That stuff is not valid.'

Verify that your validator exists by checking this in the console

ParsleyValidator.validators

Upvotes: 1

Marc-Andr&#233; Lafortune
Marc-Andr&#233; Lafortune

Reputation: 79552

Use data-parsley-stuff-list instead of data-parsley-stuffList, and data-parsley-validate instead of parsley-data-validate...

Upvotes: 0

Related Questions