user1261710
user1261710

Reputation: 2639

NativeScript - How to check the validity of the form when using RadForms?

I'm working on a new NativeScript project with Angular 8. I'm investigating the different ways we can build forms.

One option is to use the RadFroms provided by the NativeScript team.

I have installed this plugin: https://www.npmjs.com/package/nativescript-ui-dataform

I have followed the instructions on this web page: https://docs.nativescript.org/ui/components/DataForm/dataform-overview#editors

How can I check the validity of the form so I can disable the submit button?

Playground: https://play.nativescript.org/?template=play-ng&id=CQnglb&v=2

Here is a code snippet as well:

home page template

<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>

home page component

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

Upvotes: 1

Views: 346

Answers (1)

Manoj
Manoj

Reputation: 21908

Use the propertyValidated event,

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

Upvotes: 1

Related Questions