How to disable the button in Angular?

Asked

Viewed 3,335 times

0

Take a good look at the form

    <form #registerForm="ngForm" (ngSubmit)="onSubmit(registerForm)" class="col-lg-10" >
        <p>
          <label >Nome</label>
          <input type="text"  name="name" #name="ngModel" [(ngModel)]="user.name"  class="form-control" required/>
            <span *ngIf="!name.valid && name.touched">
              O nome é obrigatório
            </span>
        </p>
        <p>
          <label >Sobrenome</label>
          <input type="text"  name="surname" #surname="ngModel" [(ngModel)]="user.surname" class="form-control" required/>
            <span *ngIf="!surname.valid && surname.touched">
              O sobrenome é obrigatório
            </span>
        </p>
        <p>
          <label >Email</label>
          <input type="text"  name="email" #email="ngModel" [(ngModel)]="user.email" class="form-control" required/>
            <span *ngIf="!email.valid && email.touched">
              O email é obrigatório
            </span>
        </p>
        <p>
          <label>Senha</label>
          <input type="text"  name="password" #password="ngModel" [(ngModel)]="user.password" class="form-control" required/>
              <span *ngIf="!password.valid && password.touched">
                A senha é obrigatório
              </span>
        </p>
        <input type="submit" value="{{title}}" class="btn btn-primary" [disabled]="!registerForm.form.valid" />
    </form>
</div>

Why would the button registration was not disabled?

To know the value of !registerForm.form.Valid if it is true or false I did the following ...

In the component file I put so;

onSubmit(form: NgForm) {

    console.log(form);

  }

And I checked on the console how I’d be if I was like this;

inserir a descrição da imagem aqui

And I submitted the form and it gave this;

inserir a descrição da imagem aqui

The attribute Valid was as true knowing you should be like false

I wonder where it’s wrong?

  • 1

    checked whether the condition registerForm.form.valid is returning false?

  • how do I know?

  • if I leave so it gets disabled registerForm.form.Valid but if I leave so it is enabled ! registerForm.form.Valid

  • will need some module to install?

  • this page is in error, already repair?

  • sorry the page is not in error, I copied and pasted your suggestion in my project and did not work.

  • 1

    There is nothing wrong with your form, see: https://stackblitz.com/edit/angular-p8jtmw?file=src%2Fapp%2Fapp.component.html

  • What I would indicate is to check for something in your console with error.

  • there is no error in the consoles

  • i did an update of the post, could take a look please?

  • @wladyband you have the variable user defined in his ts?

  • I have yes, he is in my project

  • How strange I did tests onlline and everything works.

  • @Marconi could take a look, this is my project https://github.com/wladyband/epui

  • https://github.com/wladyband/epui/blob/master/src/app/registro/registro/registro.component.html

  • Please avoid long discussions in the comments; your talk was moved to the chat

Show 12 more comments

2 answers

2

You’re mixing it up Forms template with reactive Forms

Here’s a demo what it would be like

I’ll show you what the code with the reactive Forms would look like:

<form [formGroup]="registerForm" (ngSubmit)="onSubmit()" class="col-lg-10" >
        <p>
          <label >Nome</label>
          <input type="text"  name="name"  class="form-control" formControlName="name"/>
            <span  *ngIf="name.invalid && (name.dirty || name.touched)">
              O nome é obrigatório
            </span>
        </p>
        <p>
          <label >Sobrenome</label>
          <input type="text"name="surname" formControlName="surname"  class="form-control" />
            <span *ngIf="!surname.valid && surname.touched">
              O sobrenome é obrigatório
            </span>
        </p>
        <p>
          <label >Email</label>
          <input type="text"  name="email" formControlName="email" class="form-control" />
            <span *ngIf="!email.valid && email.touched">
              O email é obrigatório
            </span>
        </p>
        <p>
          <label>Senha</label>
          <input type="text"  name="password" class="form-control"  formControlName="password" />
              <span *ngIf="!password.valid && password.touched">
                A senha é obrigatório
              </span>
        </p>
        <input type="submit" value="{{title}}" class="btn btn-primary" [disabled]="registerForm.invalid" />
    </form>
</div>

NO ts

registerForm:FormGroup;
constructor(private fb: FormBuilder)

ngOnInit() {
    this.createForm()
}

createForm() {
    this.registerForm = this.fb.group({
        name: [ '', Validators.required], //Os nomes dentro do form control names tem que ser iguais a estes.
        surname: [ '', Validators.required],
        email: ['', Validators.required],
        password: ['', Validators.required]
    });
}


get name() { return this.registerForm.get('name'); }
get surname() { return this.registerForm.get('surname'); }
get email() { return this.registerForm.get('email'); }
get password() { return this.registerForm.get('password'); }
  • did not resolve, gave this error message on console Can’t bind to 'formGroup' Since it isn’t a known Property of 'form'. ("

  • Uncaught Error: Template parse errors: Can’t bind to 'formGroup' Since it isn’t a known Property of 'form'. (" <form [ERROR ->][formGroup]="registerForm" (ngSubmit)="onsubmit()" class="col-lg-10" > <p> < >"): ng://Registromodule/Registrocomponent.html@4:10 No Provider for Controlcontainer (" [ERROR ->]<form [Group]="registerForm" (ngSubmit)="onsubmit()" class="col-lg-10" > <

  • you came to test this code before you passed me?

  • I created a stackblitz for you to see.

  • @wladyband worked out well?

  • I didn’t make any changes, I just restarted the computer and started working again, but the way you put it works too.

Show 1 more comment

0

The interesting thing would be to use formGroup, that your code HTML would be much more readable, but to solve your problem based on what you are exposing, then just inform of which form you are referring, why "registerForm" It’s your fluffy, but you’re putting "registerForm.form", then understand how it can work out below:

Your input currently:

<input type="submit" value="{{title}}" class="btn btn-primary" [disabled]="!registerForm.form.valid" />

As his input should stay:

<input type="submit" value="{{title}}" class="btn btn-primary" [disabled]="!registerForm.valid" />

There, in case your problem hasn’t solved yet, then you can check exactly what was missing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.