Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom validator passed a number with requirementType: 'string' #1279

Open
markkolich opened this issue Dec 11, 2018 · 3 comments
Open

Custom validator passed a number with requirementType: 'string' #1279

markkolich opened this issue Dec 11, 2018 · 3 comments

Comments

@markkolich
Copy link

Parsley 2.8.1
jQuery 3.x

I believe this is a bug, either in code or documentation.

I'm wiring up a custom validator, to validate a simple form:

<form data-parsley-validate>
  <input type="text" required data-parsley-foobar="100"/>
  <input type="submit" />
</form>

Note the data-parsley-foobar="100" attribute on the text input field. The 100 is intentional, it looks like a number but I intend to treat it as a string in my validator.

And so, when registering my validator I'm passing requirementType: 'string' into Parsley.addValidator. However, when invoked, my validateString function is passed a requirement of type number:

Parsley.addValidator('foobar', {
  requirementType: 'string',
  validateString: function(value, requirement) {
    console.log(requirement, typeof requirement); // <<-- number???

    return false;
  },
  messages: {
    en: 'baz'
  }
});

image

I can't for the life of me figure out why my requirement is a JS number when I've specified requirementType: 'string'.

Here's a JSFiddle:
http://jsfiddle.net/rf8nyzb0/

Is this a bug, or am I completely misunderstanding the 'string' requirement parameter documentation at http://parsleyjs.org/doc/#custom-requirement ?

CC // @larrysteinke

@marcandre
Copy link
Collaborator

You're right, it's a bug. Should be easy to fix

@marcandre
Copy link
Collaborator

Sorry, taking longer than I hoped, I'm three level deeper in the rabbit-hole than I was hoping for.

Of course, in the meantime, you can use requirement = String(requirement) to circumvent the bug

@markkolich
Copy link
Author

No problem, @marcandre.

you can use requirement = String(requirement) to circumvent the bug

Yep, that's the workaround we've implemented for now.

Thanks for looking into this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants