我正在使用Alloy UI和YUI3,并且有一个表单,根据选择下拉列表中的用户选项选项,激活对服务器的ajax调用。服务器返回新的验证规则,我将其发送到AUI-Form-Validation模块。规则正在改变,但表单正在输出重复的规则。也就是说,不替换表单验证实例,而是附加到旧表单,因此我在broswer中有重复的错误字段字符串。 我想我需要销毁所有实例,但最新但似乎无法实现这一点。如何销毁/更新旧表单验证,以便我只在DOM中显示最新内容?
这是我的代码(我正在使用on.failure进行本地测试):
<!DOCTYPE html>
<html>
<head>
<title>Form Validation</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="http://cdn.alloyui.com/2.5.0/aui-css/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="container-fluid">
<div class="row-fluid">
<div class="span12">
<form id="myForm">
<select id="card-type" name="card-type" class="select_card_type">
<option value="7" selected="selected" label="Seven"></option>
<option value="2" label="Two"></option>
<option value="5" label="Five"></option>
</select>
<div class="control-group card_number">
<label class="control-label" for="cardNumber">Card number</label>
<div class="controls">
<input name="cardNumber" id="cardNumber" type="text" />
</div>
</div>
<div class="control-group csc">
<label class="control-label" for="picture">Card security code</label>
<div class="controls">
<input name="csc" id="csc" type="text">
</div>
</div>
<input class="btn btn-info" type="submit" value="Submit">
</form>
</div><!--/span-->
</div><!--/row-->
<hr>
<footer>
<p>© Company 2013</p>
</footer>
</div><!--/.fluid-container-->
<script src="http://cdn.alloyui.com/2.5.0/aui/aui-min.js"></script>
<script>
YUI().use('node', 'node-base', 'event', 'transition', 'aui-io-request', 'json-parse', 'aui-form-validator', function(Y) {
var rules;
function Validator(rules) {
this.rules = rules;
this.fieldStrings = {
cardNumber: {
required: 'Type your card number in this field.'
},
csc: {
required: 'Please provide your csc.'
}
};
this.val = new Y.FormValidator(
{
boundingBox: '#myForm',
fieldStrings: this.fieldStrings,
validateOnInput: true,
rules: this.rules
}
);
}
Y.one('.select_card_type').on('change', function(e) {
var len = Y.one('#card-type option:checked').get('value');
Y.io.request('<%= selectProductTypeResource.toString() %>', {
method: 'post',
on: {
failure: function() {
rules = {
cardNumber: {
required: true,
digits: true,
minLength: len,
maxLength: len
},
csc: {
required: true,
digits: true,
minLength: len,
maxLength: len
}
};
if (typeof (validator) === 'object') {
validator.val.destroy(true); // not working
}
validator = new Validator(rules);
}
}
});
});
});</script>
</body>
</html>
答案 0 :(得分:0)
您没有附加规则,您绑定了多个验证程序。即使您能够清除规则,旧的验证器仍然会被绑定并且不会违反规则进行验证。
为了防止&#34;销毁&#34;它,您可以创建一次验证器,然后相应地修改规则。
AlloyUI使用get/set
方法修改属性。
var rules = validator.get('rules')
- 然后修改。validator.set('rules', {cardNumber: {required: true}})
我已设置an example fiddle进行演示。如果您点击Create New Validator
和Submit
,您会看到与之相同的行为。
github上的aui-form-validator source。
答案 1 :(得分:0)
感谢回复origineil,我实施了你所提到的并让它运作起来。我使用setAttrs重置新规则,现在所有规则都按预期工作。这是我的完整工作代码:
YUI().use('node', 'node-base', 'event', 'transition', 'aui-io-request', 'json-parse', 'aui-form-validator', function(Y) {
var validator = new Y.FormValidator(
{
boundingBox: '#myForm',
validateOnInput: true
});
Y.one('.select_card_type').on('change', function(e) {
var len = Y.one('#card-type option:checked').get('value');
validator.resetAllFields();
Y.io.request('<%= selectProductTypeResource.toString() %>', {
method: 'post',
on: {
failure: function() {
var rules = {
cardNumber: {
required: true,
digits: true,
minLength: len,
maxLength: len
},
csc: {
required: true,
digits: true,
minLength: len,
maxLength: len
}
};
validator.setAttrs({rules: rules});
}
}
});
});
});