2

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.

behobu
  • 21
  • 1
  • 4

2 Answers2

1

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
Phil C
  • 962
  • 6
  • 17
0

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

Marc-André Lafortune
  • 78,216
  • 16
  • 166
  • 166
  • If I use data-parsley-stuff-list, then the validator name would have to be "stuff-list". The name isn't really the issue though - it can be named whatever (I've changed it several times) and the validator still doesn't fire. – behobu Sep 10 '15 at 14:45
  • Parsley camelizes data attributes. – Marc-André Lafortune Sep 11 '15 at 18:16
  • Actually data-parsley-stuff-list doesnt work either, it would have to be data-parsley-stuff_list. Had this problem yesterday and thats how I got my validator to work. – dubonzi Sep 15 '15 at 18:52
  • Validator name: `stuffList`. Attribute name: `data-parsley-stuff-list`. That's the recommended style. – Marc-André Lafortune Sep 15 '15 at 22:56