【问题标题】:NativeScript - How to check the validity of the form when using RadForms?NativeScript - 使用 RadForms 时如何检查表单的有效性?
【发布时间】:2020-01-23 15:28:34
【问题描述】:

我正在使用 Angular 8 开发一个新的 NativeScript 项目。我正在研究构建表单的不同方式。

一种选择是使用 NativeScript 团队提供的 RadFroms。

我已经安装了这个插件: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

这里还有一个代码 sn-p:

首页模板

<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' } ]
      }
    ]
  };

【问题讨论】:

  • @Manoj 这个可能适合你。

标签: angular forms validation nativescript


【解决方案1】:

使用propertyValidated 事件,

onDataFormLoaded(event) {
    const form = event.object;
    fromEvent(form, "propertyValidated")
        .pipe(
            takeWhile(() => !this.destroyed),
            delay(100),
        )
        .subscribe(() => {
            this.ngZone.run(() => {
                this.hasErrors = form.hasValidationErrors();
            });
        });
}

Playground Sample

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2013-03-20
    • 2022-11-07
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    相关资源
    最近更新 更多