I have a form with 3 fields: type(radio button), name and place. If I select value 'Y' from radio button validations should appear for name and place. If I select value 'N' from radio button validation should not show for place. Please help me achieve the functionality. Working stackblitz: https://stackblitz.com/edit/angular-ivy-jzjh4j?file=src%2Fapp%2Fapp.component.ts
<input type="radio" formControlName="type" name="type" value="Y"/>
Yes
<input type="radio" (change)="onChange($event)"
formControlName="type" name="type" value="N"/>
No
<small class="errormessage" *ngIf="submitted && addForm.controls.type.hasError('required')">
type is required
</small>
TS
onChange(evt)
{
var target = evt.target;
if (target.checked)
{
alert('hi');
this.addForm.controls.place.valid === true;
}
else
{
this.addForm.controls.place.valid === false;
}
}
validstate... ! A control is valid if its validators are valid. If you want a field to not be in error, remove the validator which makes it in error. Can you show youraddForminitialization ? - Random