0

Similarly to this question and this question I can't figure out how to How to configure a Parsley custom remote using Javascript, when binding an individual field.

e.g. I'm trying (simplified):

$('#field').parsley({
    remote: true,
    remoteValidator: 'mycustom';
});

being the equivalent of the example:

<input name="q" type="text" data-parsley-remote data-parsley-remote-validator='mycustom' value="foo" />

after I've registered the example remote:

window.Parsley.addAsyncValidator('mycustom', function (xhr) {
    console.log(this.$element);
    return 404 === xhr.status;
}, '/api/foo');

When this executes Parsley does try to process the remote inside the internal remote function:

validateString: function validateString(value, url, options, instance) {

While the Parsley.asyncValidators does include the mycustom remote OK, the options parameter is not the options I would expect (it's the Parsely field itself which has those options as an options property). So options.validator in this context is null, and so the method picks the default, which isn't configured, and so it errors on url.indexOf. Anyway that's probably all irrelevant if I've configured it wrong.

I've looked through the documentation, samples and source code, but cannot figure out how these values are read from the config.

Update: I installed it via bower and am using dist/parsely.min.js. I cannot see the parsely.remote.js (mentioned in the docs) anywhere in the bower build so I presume it's all compiled in.

Community
  • 1
  • 1
scipilot
  • 6,681
  • 1
  • 46
  • 65

2 Answers2

0

It turns out it was the value of the remote option needs to be "remote" not true.

$('#field').parsley({
    remote: 'remote',
    remoteValidator: 'mycustom';
});

As there is no attribute value for data-parsely-remote, I'd guessed "presence of tag" would evaluate to true, not the last word of the dashed attribute.

Marc-André Lafortune
  • 78,216
  • 16
  • 166
  • 166
scipilot
  • 6,681
  • 1
  • 46
  • 65
  • PS: To get the options of a parsley field, simply use `$('#field').parsley().options` – Marc-André Lafortune Dec 15 '16 at 14:44
  • The .options method is garbage-in-garbage-out though. It just returns what I put in. – scipilot Dec 15 '16 at 16:27
  • Yes. So are attributes. Sorry, I don't see the point, nor the relation with your question – Marc-André Lafortune Dec 15 '16 at 20:04
  • Oh, I think I get it. Posted an answer, OTH. – Marc-André Lafortune Dec 15 '16 at 20:10
  • I was saying that because the .options method just returns whatever I have set, it doesn't help me to find out what the correct settings are. For example, there is no mention in the documentation about the "remoteValidator" configuration setting - I had to guess it, from the equivalent html attribute. I only found out about the camelcase equivalence from another SO - it's not mentioned in the documentation, so the object literal configuration approach is very hard to use. – scipilot Dec 19 '16 at 16:13
  • It *is* in the documentation. It's listed as `data-parsley-remote-validator`, which corresponds to `remoteValidator` option, as explained in the "Naming" section. I read many people complaining about the documentation, I see very few PRs to make it better though :-) – Marc-André Lafortune Dec 19 '16 at 20:17
  • OK I can see that section. I didn't really glean from that the correspondence between the two ways of providing the configuration, as it's talking about how the data-attributes are converted but I can see that it must work "backwards" too. Re. PRs, when I feel I understand it enough to confidently make a contribution I will try, but currently I don't feel I can add any value. – scipilot Dec 20 '16 at 11:07
  • Sure. You're not the first to miss the correspondance section though, maybe it should be more explicit, or referenced in many other places, etc... – Marc-André Lafortune Dec 22 '16 at 05:46
0

When you define your validateString(value, url, options, instance), the options you will receive are the options of the remote validator. This validator is defined with:

  requirementType: {
    '': 'string',
    'validator': 'string',
    'reverse': 'boolean',
    'options': 'object'
  },

So there will be a validator option ('mycustom'), possibly a reverse option, and also possible a options option.

So you could bind your field with:

$('#field').parsley({
    remote: true,
    remoteValidator: 'mycustom',
    hello: 'world',
    remoteOptions: { foo: 'bar' }
});

and access 'bar' within your validator with options.options.foo or instance.options.remoteOptions.foo, and get 'world' with instance.options.hello.

Marc-André Lafortune
  • 78,216
  • 16
  • 166
  • 166
  • I'm not defining `validateString()` - it's inside the Parsley codebase. I was only inspecting there to try to guess what settings to define for the remote by trial an error. I don't want to know about the internals of the library, just the required configuration settings which aren't documented in the object literal format. – scipilot Dec 19 '16 at 16:16