如何使用 javascript 而非属性配置 Parsley 自定义遥控器
How to configure a Parsley custom remote using javascript, not attributes
与 and this question 类似,当绑定单个字段时,我不知道如何使用 Javascript 配置 Parsley 自定义遥控器。
例如我正在尝试(简化):
$('#field').parsley({
remote: true,
remoteValidator: 'mycustom';
});
相当于the example:
<input name="q" type="text" data-parsley-remote data-parsley-remote-validator='mycustom' value="foo" />
在我注册了示例遥控器之后:
window.Parsley.addAsyncValidator('mycustom', function (xhr) {
console.log(this.$element);
return 404 === xhr.status;
}, '/api/foo');
执行时,Parsley 会尝试在内部远程函数中处理远程:
validateString: function validateString(value, url, options, instance) {
虽然 Parsley.asyncValidators
包含 mycustom
远程 OK,但 options
参数不是我期望的选项(它是Parsely 字段本身具有这些选项 options
属性)。所以 options.validator
在此上下文中为空,因此该方法选择未配置的默认值,因此它在 url.indexOf
上出错。无论如何,如果我配置错误,那可能都是无关紧要的。
我查看了文档、示例和源代码,但无法弄清楚这些值是如何从配置中读取的。
更新:我通过 bower 安装并使用 dist/parsely.min.js。我在 bower 构建中的任何地方都看不到 parsely.remote.js(在文档中提到),所以我认为它都是编译的。
原来是 remote
选项的值需要 "remote" 而不是 true
。
$('#field').parsley({
remote: 'remote',
remoteValidator: 'mycustom';
});
由于 data-parsely-remote
没有属性值,我猜 "presence of tag" 会计算为 true
,而不是虚线属性的最后一个字。
当您定义 validateString(value, url, options, instance)
时,您将收到的 options
是 remote
验证器的选项。此验证器定义为:
requirementType: {
'': 'string',
'validator': 'string',
'reverse': 'boolean',
'options': 'object'
},
所以会有一个validator
选项('mycustom'),可能是一个reverse
选项,也可能是一个options
选项。
所以你可以绑定你的字段:
$('#field').parsley({
remote: true,
remoteValidator: 'mycustom',
hello: 'world',
remoteOptions: { foo: 'bar' }
});
并使用 options.options.foo
或 instance.options.remoteOptions.foo
在您的验证器中访问 'bar'
,并使用 instance.options.hello
.
获得 'world'
与
例如我正在尝试(简化):
$('#field').parsley({
remote: true,
remoteValidator: 'mycustom';
});
相当于the example:
<input name="q" type="text" data-parsley-remote data-parsley-remote-validator='mycustom' value="foo" />
在我注册了示例遥控器之后:
window.Parsley.addAsyncValidator('mycustom', function (xhr) {
console.log(this.$element);
return 404 === xhr.status;
}, '/api/foo');
执行时,Parsley 会尝试在内部远程函数中处理远程:
validateString: function validateString(value, url, options, instance) {
虽然 Parsley.asyncValidators
包含 mycustom
远程 OK,但 options
参数不是我期望的选项(它是Parsely 字段本身具有这些选项 options
属性)。所以 options.validator
在此上下文中为空,因此该方法选择未配置的默认值,因此它在 url.indexOf
上出错。无论如何,如果我配置错误,那可能都是无关紧要的。
我查看了文档、示例和源代码,但无法弄清楚这些值是如何从配置中读取的。
更新:我通过 bower 安装并使用 dist/parsely.min.js。我在 bower 构建中的任何地方都看不到 parsely.remote.js(在文档中提到),所以我认为它都是编译的。
原来是 remote
选项的值需要 "remote" 而不是 true
。
$('#field').parsley({
remote: 'remote',
remoteValidator: 'mycustom';
});
由于 data-parsely-remote
没有属性值,我猜 "presence of tag" 会计算为 true
,而不是虚线属性的最后一个字。
当您定义 validateString(value, url, options, instance)
时,您将收到的 options
是 remote
验证器的选项。此验证器定义为:
requirementType: {
'': 'string',
'validator': 'string',
'reverse': 'boolean',
'options': 'object'
},
所以会有一个validator
选项('mycustom'),可能是一个reverse
选项,也可能是一个options
选项。
所以你可以绑定你的字段:
$('#field').parsley({
remote: true,
remoteValidator: 'mycustom',
hello: 'world',
remoteOptions: { foo: 'bar' }
});
并使用 options.options.foo
或 instance.options.remoteOptions.foo
在您的验证器中访问 'bar'
,并使用 instance.options.hello
.
'world'