【问题标题】:Add Parsley.js validator with referenced field添加带有引用字段的 Parsley.js 验证器
【发布时间】:2014-05-15 07:36:46
【问题描述】:

我想为我的表单添加一些检查,这些表单的条件与其他字段的值相关(即我有一个范围表单,from 字段必须小于to 字段,反之亦然)。我在当前的验证器上没有找到类似的东西,所以我尝试自己添加。

所以,我将这两个函数添加到Assert.prototype

GreaterThanReference: function ( reference ) {
    this.__class__ = 'GreaterThanReference';
    if ( 'undefined' === typeof reference )
        throw new Error( 'GreaterThanReference must be instanciated with a value or a function' );
    this.reference = reference;
    this.validate = function ( value ) {
        var reference = 'function' === typeof this.reference ? this.reference( value ) : this.reference;
        if ( '' === value || isNaN( Number( value ) ) )
            throw new Violation( this, value, { value: Validator.errorCode.must_be_a_number } );
        if ( this.reference.value >= value )
            throw new Violation( this, value );
        return true;
    };
    return this;
}

LessThanReference: function ( reference ) {
    this.__class__ = 'LessThanReference';
    if ( 'undefined' === typeof reference )
        throw new Error( 'LessThanReference must be instanciated with a value or a function' );
    this.reference = reference;
    this.validate = function ( value ) {
        var reference = 'function' === typeof this.reference ? this.reference( value ) : this.reference;
        if ( '' === value || isNaN( Number( value ) ) )
            throw new Violation( this, value, { value: Validator.errorCode.must_be_a_number } );
        if ( this.reference.value <= value )
            throw new Violation( this, value );
        return true;
    };
    return this;
}

还有这两个给ParsleyValidator.prototype.validators:

greaterthan: function (value) {
    return $.extend(new Validator.Assert().GreaterThanReference(value), {
        priority: 256,
        requirementsTransformer: function () {
        return { name : $(value).attr('alt'), value : +$(value).val() };
    }});
}

lessthan: function (value) {
    return $.extend(new Validator.Assert().LessThanReference(value), {
        priority: 256,
        requirementsTransformer: function () {
        return { name : $(value).attr('alt'), value : +$(value).val() };
    }});
}

然后我想在引用字段上添加修订检查,这样如果我只更改引用字段的值,我仍然可以验证表单(在范围示例中,如果我从值更改,我应该验证到 field.如果我更改为值,我应该从字段中验证)。为此,我编辑了ParsleyField.prototype.addConstraint

addConstraint: function (name, requirements, priority, isDomConstraint, isReference) {
    name = name.toLowerCase();
    if ('function' === typeof window.ParsleyValidator.validators[name]) {
        var constraint = new ConstraintFactory(this, name, requirements, priority, isDomConstraint);
        // if constraint is a referenced one, I add the specular constraint on the referenced field
        if((name == 'lessthan' || name == 'greaterthan') && isReference !== true) {
            // I check if I already instanciated the referenced ParsleyField
            var referencedField = $(requirements).data('Parsley') && $(requirements).data('Parsley').__class__ == 'ParsleyField' ?
                $(requirements).data('Parsley') :
                new ParsleyField($(requirements), this.parsleyInstance).init();
            referencedField.addConstraint(name == 'lessthan' ? 'greaterthan' : 'lessthan', '#' + this.$element.attr('id'), priority, isDomConstraint, true);
        }
        // if constraint already exist, delete it and push new version
        if ('undefined' !== this.constraintsByName[constraint.name])
            this.removeConstraint(constraint.name);
        this.constraints.push(constraint);
        this.constraintsByName[constraint.name] = constraint;
    }
    return this;
}

详细地说,我添加了isReference 参数以了解我是否已经在引用字段上添加了反向约束,以避免循环引用。然后,以一种非常可怕的方式,我检查我添加的约束是否具有引用并且还不是引用的约束(也许可以通过添加某种可能是间接的(或引用的)“约束类型”来改进它) 用于检查其他字段的约束,直接用于仅检查值的约束)。如果此条件为真,我必须将新约束添加到已实例化的 ParsleyField,或者如果尚未实例化,则添加到新的 ParsleyField

这个方法有问题。如果我在引用尚未实例化的字段的字段上添加约束,则当 Parsley 的正常流程到达该字段时,它会覆盖它,从而消除我之前添加的约束。

零问题:这是实现我想要的正确方法吗?我承认我没有过多地探索 Parsley API,但我想使用尽可能少的功能,因为我使用相同的检查服务器端,我应该能够在双方做同样的事情。

如果零答案是“是”,我该如何解决覆盖问题?可能我应该能够在 ParsleyField 实例化上检查它是否已经被实例化,但是如何?

【问题讨论】:

    标签: javascript validation parsley.js


    【解决方案1】:

    我不知道您使用的是哪个版本的 parsley js,也许我的回答现在不实际。但是,我在使用 v.2.0.2 时遇到了同样的问题。幸运的是,文档包含编写自定义验证器的教程。 请检查以下https://github.com/mvpotter/parsley-extra-validators。如果我对您的理解正确,他们会完全按照您的需要做。感谢您提供任何反馈。

    【讨论】:

    • 哦,谢谢! ^_^ 在我写这个问题的时候,Parsley 2.0 没有额外的验证器,我会尽快检查出来。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-25
    • 2017-03-17
    • 1970-01-01
    相关资源
    最近更新 更多