输入值不在 parsley.js 的 addAsyncValidator 方法参数中生效。

huangapple go评论92阅读模式
英文:

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

        &lt;form action=&quot;/edit/email&quot; method=&quot;POST&quot; id=&quot;my_form&quot;&gt;
            &lt;div class=&quot;row&quot;&gt;
                &lt;div class=&quot;col-lg-4&quot;&gt;
                    &lt;div class=&quot;form-group&quot;&gt;
                        &lt;label for=&quot;email&quot;&gt;Email&lt;/label&gt;
                        &lt;input type=&quot;text&quot;
                               class=&quot;form-control&quot;
                               name=&quot;email&quot;
                               id=&quot;email&quot;
                               {{/* value=&quot;{{.User.Email}}&quot; */}}
                               value=&quot;foo@bar.com&quot;
                               data-parsley-required=&quot;true&quot;
                               data-parsley-type=&quot;email&quot;
                               data-parsley-whitespace=&quot;trim&quot;
                               data-parsley-trigger=&quot;change&quot;
                               data-parsley-remote
                               data-parsley-remote-validator=&quot;checkEmailTaken&quot;
                               data-parsley-remote-message=&quot;&quot;&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            &lt;button role=&quot;button&quot; type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Submit&lt;/button&gt;
        &lt;/form&gt;

JQuery

    $(function () {
        $(&quot;#my_form&quot;).parsley();

        $(&quot;#my_form&quot;).on(&quot;submit&quot;, function (e) {
            e.preventDefault();
            makeFormRequest(this); // Ajax call
        });

        Parsley.addAsyncValidator(&quot;checkEmailTaken&quot;, function (xhr) {
            if (404 === xhr.status) {
                r = $.parseJSON(xhr.responseText);
                this.addError(&quot;remote&quot;, { message: r.error });
            }
            return 200 === xhr.status;
        }, &quot;/check/email/taken&quot;, {
                &quot;data&quot;: {
                    &quot;email&quot;: $(&quot;#email&quot;).val(), // The problem here is the val is always &quot;foo@bar.com&quot; even when input value is changed by the user
                    &quot;id&quot;: &quot;{{.User.ObjectID.Hex}}&quot;
                }
            });
    });

答案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

{
    &quot;data&quot;: {
        &quot;email&quot;: $(&quot;#email&quot;).val(),
        &quot;id&quot;: &quot;{{.User.ObjectID.Hex}}&quot;
    }
}

Is evaluated to this

{
    &quot;data&quot;: {
        &quot;email&quot;: &quot;foo@bar.com&quot;,
        &quot;id&quot;: &quot;some hex value&quot;
    }
}

...and then passed to the addAsyncValidator as the last argument.

To put in another way, $(&quot;#email&quot;).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 () {
    $(&quot;#my_form&quot;).parsley();

    $(&quot;#my_form&quot;).on(&quot;submit&quot;, function (e) {
        e.preventDefault();
        makeFormRequest(this); // Ajax call
    });

    Parsley.addAsyncValidator(&quot;checkEmailTaken&quot;, function (xhr) {
        if (404 === xhr.status) {
            r = $.parseJSON(xhr.responseText);
            this.addError(&quot;remote&quot;, { message: r.error });
        }
        return 200 === xhr.status;
    }, &quot;/check/email/taken&quot;, {
        &quot;data&quot;: {
            &quot;id&quot;: &quot;{{.User.ObjectID.Hex}}&quot;
        }
    });
});

huangapple
  • 本文由 发表于 2017年5月13日 19:10:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/43952423.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定