I've this simple form on my Angular component.
user: UserDTO;
name: FormControl;
attendee: FormControl;
attendeeForm: FormGroup;
constructor(
private fb: FormBuilder,
) {
this.user = new UserDTO('', '', '', 2, true);
this.name = new FormControl(this.user.name, [
Validators.required,
Validators.minLength(3),
Validators.maxLength(55),
]);
this.attendee = new FormControl(this.user.attendee, Validators.required);
this.attendeeForm = this.fb.group({
name: this.name,
status: 2,
attendee: this.attendee,
});
}
And this is the HTML:
<h2>Formulari de registre</h2>
<form (ngSubmit)="registerAttendee()" [formGroup]="attendeeForm" class="form-group">
<mat-form-field >
<input type="name" matInput placeholder="Name" required [formControl]="name">
</mat-form-field>
<div *ngIf="name.errors">
<span style="color: red" *ngIf="name.errors && (name.touched || name.dirty)">
<span *ngIf="name.errors['required']">El nom és requerit</span>
<span *ngIf="name.errors['minlength']">El nom ha de tenir al menys tres caràcters</span>
<span *ngIf="name.errors['maxlength']">El nom pot tenir màxim 55 caràcters</span>
</span>
</div>
<p>Assistiràs?</p>
<mat-radio-group aria-label="attendee">
<mat-radio-button [value]="true" [formControl]="attendee">Sí</mat-radio-button>
<mat-radio-button [value]="false" [formControl]="attendee">No</mat-radio-button>
</mat-radio-group>
<button type="submit" mat-raised-button color="primary [disabled]="!attendeeForm.valid">
Enviar
</button>
</form>
How can I get the value of this radio button?
The result is always true because it takes it from new User by default. I have tried to do the same without starting the user but it doesn't work either. I have also read that the values received from a radio-button are of type "string", maybe this is the problem.
Surely it is a simple question but I don't know how to achieve it.
this.attendeeForm.value.attendee