gof*_*der 5 javascript validation parsley.js
我想为我的表单添加一些检查,其条件与其他字段的值相关(即我有一个范围形式,from字段必须小于to字段,反之亦然).我在目前的Validators上找不到类似的东西,所以我试着自己添加.
所以,我将这两个函数添加到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;
}
Run Code Online (Sandbox Code Playgroud)
和
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;
}
Run Code Online (Sandbox Code Playgroud)
以及其他两个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() };
}});
}
Run Code Online (Sandbox Code Playgroud)
和
lessthan: function (value) {
return $.extend(new Validator.Assert().LessThanReference(value), {
priority: 256,
requirementsTransformer: function () {
return { name : $(value).attr('alt'), value : +$(value).val() };
}});
}
Run Code Online (Sandbox Code Playgroud)
然后我想在引用的字段上添加启用的检查,这样如果我只更改引用字段的值,我仍然可以验证表单(在范围示例中,如果我从值更改,我应该验证到字段.如果我改变价值,我应该从现场验证).为此,我编辑了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;
}
Run Code Online (Sandbox Code Playgroud)
详细地说,我添加了isReference参数以了解我是否已经在引用字段上添加了反向约束,以避免循环引用.然后,以一种非常可怕的方式,我检查我正在添加的约束是否有引用并且还不是引用的约束(也许可以通过添加某种"约束类型"来改进它,它可以是间接的(或引用的) )对于检查其他字段的约束,并指向那些只检查值的人).如果这个条件为真,我必须将新约束添加到已经实例化的ParsleyField,或者如果尚未实例化,则添加到新的约束ParsleyField.
这种方法有问题.如果我在引用尚未实例化的字段的字段上添加约束,当Parsley的正常流程到达该字段时,它会覆盖它,从而消除我之前添加的约束.
零问题:这是实现我想要的正确方法吗?我承认我没有太多探索Parsley API,但我想尽可能少使用功能,因为我使用相同的检查服务器端,我应该能够在两边做同样的事情.
如果零回答为"是",我该如何解决覆盖问题?可能我应该能够在ParsleyField实例化时检查它是否已经实例化,但是如何实现?
小智 1
我不知道你使用的是哪个版本的欧芹js,也许我的答案现在并不实际。然而,我在 v.2.0.2 中遇到了同样的问题。幸运的是,文档包含编写自定义验证器的教程。您能否检查以下内容https://github.com/mvpotter/parsley-extra-validators。如果我理解正确的话,他们正是按照您的需要做的。如有任何反馈,我们将不胜感激。
| 归档时间: |
|
| 查看次数: |
1441 次 |
| 最近记录: |