英文:
input value not live on parsley.js addAsyncValidator method param
问题
我正在努力获取输入的值(实时),并将其作为数据传递给addAsyncValidator方法。但是,addAsyncValidator始终获取的是页面渲染时在value属性中设置的值,而不是输入更改后的值。以下是你的代码:
标记
<form action="/edit/email" method="POST" id="my_form">
<div class="row">
<div class="col-lg-4">
<div class="form-group">
<label for="email">Email</label>
<input type="text"
class="form-control"
name="email"
id="email"
{{/* value="{{.User.Email}}" */}}
value="foo@bar.com"
data-parsley-required="true"
data-parsley-type="email"
data-parsley-whitespace="trim"
data-parsley-trigger="change"
data-parsley-remote
data-parsley-remote-validator="checkEmailTaken"
data-parsley-remote-message="">
</div>
</div>
</div>
<button role="button" type="submit" class="btn btn-primary">Submit</button>
</form>
JQuery
$(function () {
$("#my_form").parsley();
$("#my_form").on("submit", function (e) {
e.preventDefault();
makeFormRequest(this); // Ajax call
});
Parsley.addAsyncValidator("checkEmailTaken", function (xhr) {
if (404 === xhr.status) {
r = $.parseJSON(xhr.responseText);
this.addError("remote", { message: r.error });
}
return 200 === xhr.status;
}, "/check/email/taken", {
"data": {
"email": $("#email").val(), // 这里的问题是val始终是"foo@bar.com",即使用户更改了输入值
"id": "{{.User.ObjectID.Hex}}"
}
});
});
希望这可以帮助到你。如果你有任何其他问题,请随时问我。
英文:
I'm having a hard time getting the value (live) of an input and pass it on addAsyncValidator method as a data. The addAsyncValidator always gets the value that has been set on the value attribute of an input when the page is rendered instead of the value of the input when it is changed. Here's my code:
Markup
<form action="/edit/email" method="POST" id="my_form">
<div class="row">
<div class="col-lg-4">
<div class="form-group">
<label for="email">Email</label>
<input type="text"
class="form-control"
name="email"
id="email"
{{/* value="{{.User.Email}}" */}}
value="foo@bar.com"
data-parsley-required="true"
data-parsley-type="email"
data-parsley-whitespace="trim"
data-parsley-trigger="change"
data-parsley-remote
data-parsley-remote-validator="checkEmailTaken"
data-parsley-remote-message="">
</div>
</div>
</div>
<button role="button" type="submit" class="btn btn-primary">Submit</button>
</form>
JQuery
$(function () {
$("#my_form").parsley();
$("#my_form").on("submit", function (e) {
e.preventDefault();
makeFormRequest(this); // Ajax call
});
Parsley.addAsyncValidator("checkEmailTaken", function (xhr) {
if (404 === xhr.status) {
r = $.parseJSON(xhr.responseText);
this.addError("remote", { message: r.error });
}
return 200 === xhr.status;
}, "/check/email/taken", {
"data": {
"email": $("#email").val(), // The problem here is the val is always "foo@bar.com" even when input value is changed by the user
"id": "{{.User.ObjectID.Hex}}"
}
});
});
答案1
得分: 1
当你的应用程序首次加载时,电子邮件始终与输入中的内容相同的原因是,这是你将作为options
对象的最后一个参数传递给addAsyncValidator
的值。
这意味着以下代码:
{
"data": {
"email": $("#email").val(),
"id": "{{.User.ObjectID.Hex}}"
}
}
被计算为:
{
"data": {
"email": "foo@bar.com",
"id": "some hex value"
}
}
...然后作为最后一个参数传递给addAsyncValidator
。
换句话说,$("#email").val()
只计算一次,即在你的js应用程序启动时,它不会在验证器发出xhr请求时每次计算。
现在关于如何实现你想要的功能,请注意我不是Parsley.js的用户,所以我可能会有误解,但从文档中看,我觉得:
你想要的电子邮件的值已经自动发送了,但因为你传递了具有相同email
键的选项对象,你实际上用你不想要的值覆盖了你想要的值。
关于data-parsley-remote
的文档说(重点在于):
> 定义用于验证输入内容的调用的URL。
> 例如,data-parsley-remote="http://url.ext"。如果URL包含字符串"{value}",该值将替换URL中的字符串(典型的RESTful API),否则该值将作为数据参数传递,键为输入的名称或ID。
所以如果我正确理解文档,只需要这样做就足够了:
$(function () {
$("#my_form").parsley();
$("#my_form").on("submit", function (e) {
e.preventDefault();
makeFormRequest(this); // Ajax call
});
Parsley.addAsyncValidator("checkEmailTaken", function (xhr) {
if (404 === xhr.status) {
r = $.parseJSON(xhr.responseText);
this.addError("remote", { message: r.error });
}
return 200 === xhr.status;
}, "/check/email/taken", {
"data": {
"id": "{{.User.ObjectID.Hex}}"
}
});
});
英文:
The reason the email is always the same as what's in the input when your app is first loaded is because that's the value that you are passing in the options
object as the last argument to addAsyncValidator
.
That means that this code
{
"data": {
"email": $("#email").val(),
"id": "{{.User.ObjectID.Hex}}"
}
}
Is evaluated to this
{
"data": {
"email": "foo@bar.com",
"id": "some hex value"
}
}
...and then passed to the addAsyncValidator
as the last argument.
To put in another way, $("#email").val()
is evaluated only once and that's at the time when your js app starts, it is not evaluated every time the validator makes the xhr request.
Now as to how to do what you want, please note that I'm not a user of Parsley.js so I may be mistaken here, but from looking at the documentation it seems to me that:
The email's value that you want is already being sent automatically but because you're passing the options object with the same email
key you're effectively overwriting the value you want with the value you don't want.
The docs on data-parsley-remote
say (emphasis mine):
> Define the url that will be called to validate the entered content.
> e.g. data-parsley-remote="http://url.ext". If the url contains the
> string "{value}", the value will replace it in the URL (typical of
> RESTful APIs), otherwise the value will be passed as a data parameter,
> with the key being the input's name or ID.
So if I understand the docs correctly it should be enough to do this:
$(function () {
$("#my_form").parsley();
$("#my_form").on("submit", function (e) {
e.preventDefault();
makeFormRequest(this); // Ajax call
});
Parsley.addAsyncValidator("checkEmailTaken", function (xhr) {
if (404 === xhr.status) {
r = $.parseJSON(xhr.responseText);
this.addError("remote", { message: r.error });
}
return 200 === xhr.status;
}, "/check/email/taken", {
"data": {
"id": "{{.User.ObjectID.Hex}}"
}
});
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论