1

Hello I have this form:

<form (ngSubmit)="create()" #form="ngForm">
<input type="text" id="name" class="form-control" [(ngModel)]="data.name"
 [ngModelOptions]="{standalone: true}" #name="ngModel" required>
<p *ngIf="name.invalid && (name.dirty || name.touched) && 
 name.errors.required" class="text-danger">Error</p>

<button type="submit" class="btn btn-primary btn-block btn-flat" 
  [disabled]="form.invalid">Send</button>
</form>

And I have a problem with validations. Validation is ok on input, but button is still enabled. I would like to disable button when input is empty. How to disable button when form is invalid? Thanks

1 Answer 1

1

In order for the input element to be considered in the form validation, you should remove the standalone option and give a name attribute to the element:

<input type="text" name="nameInput" [(ngModel)]="data.name" #name="ngModel" required ...>

See this stackblitz for a demo.


Here is a description of the standalone option given in the Angular documentation:

standalone: Defaults to false. If this is set to true, the ngModel will not register itself with its parent form, and will act as if it's not in the form.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.