NativeScript - 使用 RadForms 时如何检查表单的有效性?
NativeScript - How to check the validity of the form when using RadForms?
我正在使用 Angular 8 开发一个新的 NativeScript 项目。我正在研究构建表单的不同方式。
一种选择是使用 NativeScript 团队提供的 RadFrom。
我安装了这个插件:https://www.npmjs.com/package/nativescript-ui-dataform
我已按照此网页上的说明进行操作:https://docs.nativescript.org/ui/components/DataForm/dataform-overview#editors
如何检查表单的有效性以便禁用提交按钮?
游乐场:https://play.nativescript.org/?template=play-ng&id=CQnglb&v=2
这里还有一个代码片段:
主页模板
<StackLayout>
<Labels text="Home Page"></Labels>
<RadDataForm [source]="source" [metadata]="metadata"></RadDataForm>
<Button text="Log In" (tap)="onLogin()" class="btn btn-primary"
isEnabled="{{ f.valid === true }}">
</Button>
</StackLayout>
主页组件
export class HomeComponent implements OnInit {
ngOnInit(): void {
}
public source = {
username: '',
password: ''
};
public metadata = {
isReadOnly: false,
propertyAnnotations: [
{
name: 'username',
displayName: 'Username',
editor: 'Text',
validators: [ { name: 'NonEmpty' } ]
},
{
name: 'password',
displayName: 'Password',
editor: 'Password',
validators: [ { name: 'NonEmpty' } ]
}
]
};
使用propertyValidated
事件,
onDataFormLoaded(event) {
const form = event.object;
fromEvent(form, "propertyValidated")
.pipe(
takeWhile(() => !this.destroyed),
delay(100),
)
.subscribe(() => {
this.ngZone.run(() => {
this.hasErrors = form.hasValidationErrors();
});
});
}
我正在使用 Angular 8 开发一个新的 NativeScript 项目。我正在研究构建表单的不同方式。
一种选择是使用 NativeScript 团队提供的 RadFrom。
我安装了这个插件:https://www.npmjs.com/package/nativescript-ui-dataform
我已按照此网页上的说明进行操作:https://docs.nativescript.org/ui/components/DataForm/dataform-overview#editors
如何检查表单的有效性以便禁用提交按钮?
游乐场:https://play.nativescript.org/?template=play-ng&id=CQnglb&v=2
这里还有一个代码片段:
主页模板
<StackLayout>
<Labels text="Home Page"></Labels>
<RadDataForm [source]="source" [metadata]="metadata"></RadDataForm>
<Button text="Log In" (tap)="onLogin()" class="btn btn-primary"
isEnabled="{{ f.valid === true }}">
</Button>
</StackLayout>
主页组件
export class HomeComponent implements OnInit {
ngOnInit(): void {
}
public source = {
username: '',
password: ''
};
public metadata = {
isReadOnly: false,
propertyAnnotations: [
{
name: 'username',
displayName: 'Username',
editor: 'Text',
validators: [ { name: 'NonEmpty' } ]
},
{
name: 'password',
displayName: 'Password',
editor: 'Password',
validators: [ { name: 'NonEmpty' } ]
}
]
};
使用propertyValidated
事件,
onDataFormLoaded(event) {
const form = event.object;
fromEvent(form, "propertyValidated")
.pipe(
takeWhile(() => !this.destroyed),
delay(100),
)
.subscribe(() => {
this.ngZone.run(() => {
this.hasErrors = form.hasValidationErrors();
});
});
}