成功提交后重置 aurelia-validation
Resetting aurelia-validation after a successful submit
我正在为我的应用程序使用 aurelia-validation。我想做的是在成功提交后将表单重置为空值。
这是我的看法class:
@inject(Validation)
export class Inquire {
@ensure(isName)
firstName = '';
@ensure(isName)
lastName = '';
. . .
constructor(validation) {
this.validation = validation.on(this);
};
sendInquiry() {
this.validation.validate()
.then(() => {
// make API call here, which works
// reset fields
this.firstName = '';
. . .
}).catch((validationResult) => {
console.log(validationResult);
});
};
};
如果我将 firstName
、lastName
和其他字段设置回空字符串,则会重新触发表单验证。我怎样才能防止这种情况发生?
您似乎可以在 ValidationGroup
上调用 .clear()
或 .destroy()
作为 referenced here in the source code,但这对我不起作用。
ValidationGroup
的 clear()
方法应该可以解决问题。关键是你必须在 之后调用这个 你已经清除了你的字段:
sendInquiry() {
this.validation.validate()
.then(() => {
// make API call here, which works
// reset fields
this.firstName = '';
. . .
//this resets the validation and clears the error messages
this.validation.clear();
}).catch((validationResult) => {
console.log(validationResult);
});
};
在较新的 Aurelia 验证版本中(2016 年 8 月之后),您需要使用 reset
方法:
this.validation.reset()
The opposite of the validate
method is reset
. Calling reset with no arguments will unrender any previously rendered validation results. You can supply a reset instruction to limit the reset to a specific object or property:
controller.reset();
controller.reset({ object: person });
controller.reset({ object: person, propertyName: 'firstName' });
我正在为我的应用程序使用 aurelia-validation。我想做的是在成功提交后将表单重置为空值。
这是我的看法class:
@inject(Validation)
export class Inquire {
@ensure(isName)
firstName = '';
@ensure(isName)
lastName = '';
. . .
constructor(validation) {
this.validation = validation.on(this);
};
sendInquiry() {
this.validation.validate()
.then(() => {
// make API call here, which works
// reset fields
this.firstName = '';
. . .
}).catch((validationResult) => {
console.log(validationResult);
});
};
};
如果我将 firstName
、lastName
和其他字段设置回空字符串,则会重新触发表单验证。我怎样才能防止这种情况发生?
您似乎可以在 ValidationGroup
上调用 .clear()
或 .destroy()
作为 referenced here in the source code,但这对我不起作用。
ValidationGroup
的 clear()
方法应该可以解决问题。关键是你必须在 之后调用这个 你已经清除了你的字段:
sendInquiry() {
this.validation.validate()
.then(() => {
// make API call here, which works
// reset fields
this.firstName = '';
. . .
//this resets the validation and clears the error messages
this.validation.clear();
}).catch((validationResult) => {
console.log(validationResult);
});
};
在较新的 Aurelia 验证版本中(2016 年 8 月之后),您需要使用 reset
方法:
this.validation.reset()
The opposite of the
validate
method isreset
. Calling reset with no arguments will unrender any previously rendered validation results. You can supply a reset instruction to limit the reset to a specific object or property:
controller.reset();
controller.reset({ object: person });
controller.reset({ object: person, propertyName: 'firstName' });