javascript - 添加带有引用字段的 Parsley.js 验证器

标签 javascript validation parsley.js

我想为我的表单添加一些检查,这些表单的条件与其他字段的值相关(即我有一个范围表单并且 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() };
    }});
}

然后我想在引用字段上添加 revesed 检查,这样如果我只更改引用字段的值我仍然可以验证表单(在范围示例中,如果我从值更改,我应该验证到字段。如果我更改为值,我应该从字段中验证)。为此,我编辑了 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,但我想使用尽可能少的功能,因为我在服务器端使用相同的检查,我应该能够在两边做同样的事情。

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

最佳答案

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

关于javascript - 添加带有引用字段的 Parsley.js 验证器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22838235/

相关文章:

javascript - 如何使用 Angular js 过滤数组

python - 在python中高效读取和验证csv文件

javascript - Parsley.js 和 Laravel 表单验证错误

javascript - ParlseyJS - 删除禁用字段的验证

javascript - 如何使用 jquery 在 Bootstrap 5 中创建和修改弹出窗口?

jquery - JavaScript/jQuery - 参数 "Array"- 可能吗?

可扩展的 HTML 验证器

angularjs - Angular 2 Parsley 在输入无效时阻止表单提交

javascript - 将 JPlayer 的播放列表与播放器分开以启用移动设备滚动

php - Codeigniter 的正则表达式匹配