I'm currently working on validating a form using this link. The requirement is simple - when an input field is invalid, the `save` button should be disabled. Conversely, when all input fields are valid, the `SAVE` button should be enabled/shown. The 'Add' functionality is working as expected, but I'm facing issues with the 'Update' functionality. The 'SAVE' button is not appearing when all input fields are valid, and I'm unsure why.
<!--upd-->
<div *ngIf="updateEnable" align="center">
<form align="center" ngNativeValidate>
id: <input disabled name="id" #updId="ngModel" [value]="selectedStudent.id" [(ngModel)]="selectedStudent.id" class="form-control" required/>
<br> name: <input name="name" #updName="ngModel" [value]="selectedStudent.name" [(ngModel)]="selectedStudent.name" class="form-control" required [rangeLength]="[3, 30]" />{{updName.valid}}, {{updName.touched}}, {{updName.pristine}}
<p *ngIf="updName.invalid || updName.errors?.rangeLength">Name must be filled and must be between 3-30 characters long</p>
<br> year: <input name="year" #updYear="ngModel" [value]="selectedStudent.year" [(ngModel)]="selectedStudent.year" class="form-control" required digits/>{{updYear.valid}}, {{updYear.touched}}, {{updYear.pristine}}
<p *ngIf="updYear.invalid || updYear.errors?.digits">Year must be filled and must be a number</p>
<br> semester: <input name="semester" #updSemester="ngModel" [value]="selectedStudent.semester" [(ngModel)]="selectedStudent.semester" class="form-control" required digits/>{{updSemester.valid}}, {{updSemester.touched}}, {{updSemester.pristine}}
<p *ngIf="updSemester.invalid || updSemester.errors?.digits">Semester must be filled and must be a number</p>
<br>Major:
<select name="major" #updMajor="ngModel" [value]="selectedStudent.major" [(ngModel)]="selectedStudent.major" class="form-control" required>{{updMajor.valid}}
<option class="form-control" value="Computer Science">Computer Science</option>
<option class="form-control" value="Politic">Politic</option>
<option class="form-control" value="Accounting">Accounting</option>
</select>
<p *ngIf="updMajor.invalid">Major must be filled</p>
<br> score: <input name="score" #updScore="ngModel" [value]="selectedStudent.score" [(ngModel)]="selectedStudent.score" class="form-control" required digits [range]="[0, 101]" />{{updScore.valid}}, {{updScore.touched}}, {{updScore.pristine}}
<p *ngIf="updScore.invalid || updScore.errors?.digits || updScore.errors?.range">Score must be filled, must be a number, and must be between 0-100</p>
<br> email: <input name="email" #updEmail="ngModel" [value]="selectedStudent.email" [(ngModel)]="selectedStudent.email" class="form-control" required email/>{{updEmail.valid}}, {{updEmail.touched}}, {{updEmail.pristine}}
<p *ngIf="updEmail.invalid || updEmail.errors?.email">Email must be filled and must contain @ and domain</p>
<br>
<div *ngIf="updId.invalid || updName.invalid || updYear.invalid || updSemester.invalid || updMajor.invalid || updScore.invalid || updEmail.invalid">
<button disabled class="btn btn-warning" title="Cannot save, please check error message above" type="button" (click)="updStudent(updId.value , updName.value, updYear.value, updSemester, updMajor.value , updScore.value, updEmail.value)" data-dismiss="modal">SAVE</button>
</div>
<div *ngIf="updId.valid && updName.valid && updYear.valid && updSemester.valid && updMajor.valid && updScore.valid && updEmail.valid">
<button disabled class="btn btn-warning" type="button" (click)="updStudent(updId.value , updName.value, updYear.value, updSemester, updMajor.value , updScore.value, updEmail.value)" data-dismiss="modal">SAVE</button>
</div>
<button class="btn btn-secondary" #notSaved type="button" data-dismiss="modal">CANCEL</button>
</form>
</div>
Here is the form for inserting data that is functioning correctly.
<form align="center" ngNativeValidate>
ID:
<input #newId="ngModel" name="id" class="form-control" [(ngModel)]="newId.value" required digits/>Valid?{{newId.valid}}, Clean?{{newId.pristine}}, Touched?{{newId.touched}}
<p *ngIf="newId.invalid || newId.errors?.digits">ID must be filled and must be numbers</p>
<br>Name:
<input #newName="ngModel" name="name" class="form-control" [(ngModel)]="newName.value" required [rangeLength]="[3, 30]"/>Valid?{{newName.pristine}}, Clean?{{newName.pristine}}, Touched?{{newName.touched}}
<p *ngIf="newName.invalid || newName.errors?.rangeLength">Name must be filled and must be between 3-30 characters long</p>
<br>Year:
<input #newYear="ngModel" name="year" class="form-control" [(ngModel)]="newYear.value" required digits/>Valid?{{newYear.valid}}, Clean?{{newYear.pristine}}, Touched?{{newYear.touched}}
<p *ngIf="newYear.invalid || newYear.errors?.digits">Year must be filled and must be numbers</p>
<br>Semester:
<input #newSemester="ngModel" name="semester" class="form-control" [(ngModel)]="newSemester.value" required digits/>Valid?{{newSemester.valid}}, Clean?{{newSemester.pristine}}, Touched?{{newSemester.touched}}
<p *ngIf="newSemester.invalid || newSemester.errors?.digits">Semester must be filled and must be numbers</p>"
<br>Major:
<select #newMajor="ngModel" name="major" [(ngModel)]="newMajor.value" class="form-control" required>Valid?{{newMajor.valid}}, Clean?{{newMajor.pristine}}, Touched?{{newMajor.touched}}
<option value="Computer Science">Computer Science</option>
<option value="Politician">Political Science</option>
<option value="Accounting">Accounting</option>
</select>
<p *ngIf="newMajor.invalid">Major must be filled</p>
<br>Score:
<input #newScore="ngModel" name="score" class="form-control" [(ngModel)]="newScore.value" required digits [range]="[0, 101]">Valid?{{newScore.valid}}, Clean?{{newScore.pristine}}, Touched?{{newScore.touched}}
<p *ngIf="newScore.invalid || newScore.errors?.digits || newScore.errors?.range">Score must be filled, must be numbers, and must be between 0-100</p>
<br>Email:
<input #newEmail="ngModel" name="email" class="form-control" [(ngModel)]="newEmail.value" required email/>Valid?{{newEmail.valid}}, Clean?{{newEmail.pristine}}, Touched?{{newEmail.touched}}
<p *ngIf="newEmail.invalid || newEmail.errors?.email">Email must contain @ and domain</p>
<!--if condition invalid-->
<div *ngIf="newId.invalid || newName.invalid || newYear.invalid || newSemester.invalid || newMajor.invalid || newScore.invalid || newEmail.invalid">
<br><button disabled title="Cannot save, please check error message above" class="btn btn-primary" type="submit" (click)="addStudent(newId.value, newName.value, newYear.value, newSemester.value, newMajor.value, newScore.value, newEmail.value)">SAVE</button>
</div>
<!--if condition valid-->
<div *ngIf="newId.valid && newName.valid && newYear.valid && newSemester.valid && newMajor.valid && newScore.valid && newEmail.valid">
<br><button class="btn btn-primary" type="submit" (click)="addStudent(newId.value, newName.value, newYear.value, newSemester.value, newMajor.value, newScore.value, newEmail.value)" data-dismiss="modal">SAVE</button>
</div>
<button class="btn btn-secondary" data-dismiss="modal">CANCEL</button>
</form>
<div *ngIf="updateEnable" align="center">
<form align="center" ngNativeValidate>
id: <input disabled name="id" #updId="ngModel" [value]="selectedStudent.id" [(ngModel)]="selectedStudent.id" class="form-control" required/>
<br>
name: <input name="name" #updName="ngModel" [value]="selectedStudent.name" [(ngModel)]="selectedStudent.name" class="form-control" required [rangeLength]="[3, 30]"/>{{updName.valid}}, {{updName.touched}}, {{updName.pristine}}
<p *ngIf="updName.invalid || updName.errors?.rangeLength">Name must be filled and must be between 3-30 characters long</p>
<br>
year: <input name="year" #updYear="ngModel" [value]="selectedStudent.year" [(ngModel)]="selectedStudent.year" class="form-control" required digits/>{{updYear.valid}}, {{updYear.touched}}, {{updYear.pristine}}
<p *ngIf="updYear.invalid || updYear.errors?.digits">Year must be filled and must be a number</p>"
<br>
semester: <input name="semester" #updSemester="ngModel" [value]="selectedStudent.semester" [(ngModel)]="selectedStudent.semester" class="form-control" required digits/>{{updSemester.valid}}, {{updSemester.touched}}, {{updSemester.pristine}}
<p *ngIf="updSemester.invalid || updSemester.errors?.digits">Semester must be filled and must be a number</p>
<br>Major:
<select name="major" #updMajor="ngModel" [value]="selectedStudent.major" [(ngModel)]="selectedStudent.major" class="form-control" required>{{updMajor.valid}}
<option class="form-control" value="Computer Science">Computer Science</option>
<option class="form-control" value="Political Science">Political Science</option>
<option class="form-control" value="Accounting">Accounting</option>
</select>
<p *ngIf="updMajor.invalid">Major must be filled</p>
<br>
score: <input name="score" #updScore="ngModel" [value]="selectedStudent.score" [(ngModel)]="selectedStudent.score" class="form-control" required digits [range]="[0, 101]"/>{{updScore.valid}}, {{updScore.touched}}, {{updScore.pristine}}
<p *ngIf="updScore.invalid || updScore.errors?.digits || updScore.errors?.range">Score must be filled, must be a number, and must be between 0-100</p>
<br>
email: <input name="email" #updEmail="ngModel" [value]="selectedStudent.email" [(ngModel)]="selectedStudent.email" class="form-control" required email/>{{updEmail.valid}}, {{updEmail.touched}}, {{updEmail.pristine}}
<p *ngIf="updEmail.invalid || updEmail.errors?.email">Email must be filled and must contain @ and domain</p>
<br>
<div *ngIf="updId.invalid || updName.invalid || updYear.invalid || updSemester.invalid || updMajor.invalid || updScore.invalid || updEmail.invalid">
<button disabled class="btn btn-warning" title="Cannot save, please check error message above" type="button" (click)="updStudent(updId.value , updName.value, updYear.value, updSemester, updMajor.value , updScore.value, updEmail.value)" data-dismiss="modal">SAVE</button>
</div>
<div *ngIf="updId.valid && updName.valid && updYear.valid && updSemester.valid && updMajor.valid && updScore.valid && updEmail.valid">
<button disabled class="btn btn-warning" type="button" (click)="updStudent(updId.value , updName.value, updYear.value, updSemester, updMajor.value , updScore.value, updEmail.value)" data-dismiss="modal">SAVE</button>
</div>
<button class="btn btn-secondary" #notSaved type="button" data-dismiss="modal">CANCEL</button>
</form>
</div>
If you need more snippets or information, feel free to ask. Thank you!