Within my Angular app, I have implemented a reactive form containing three checkboxes:
- The first checkbox allows users to choose whether they want to remain anonymous. If left unchecked, the name field will be visible. If checked, the name field will be hidden.
- The second checkbox enables users to reveal an email field. Once checked, the email field becomes visible; otherwise, it remains hidden.
- The third checkbox, labeled 'Agree', must be selected by the user in order to submit the form successfully.
Despite meeting all specified criteria, the submission form continues to be invalid. My aim is to ensure that submissionForm.valid
returns true
. Below is the code snippet for my submission form component:
I encountered an issue where all fields are returning null values, even after proper initialization. Could someone provide guidance on how to address this issue? Although I believe I have set up everything correctly, I am perplexed as to why the values are still showing as null.
import { Component, OnInit } from '@angular/core';
import { FormGroup, Validators, FormBuilder } from '@angular/forms';
@Component({
selector: 'app-submission',
templateUrl: './submission-form.component.html',
styleUrls: [
'./submission-form.component.scss'
]
})
export class SubmissionFormComponent implements OnInit {
submissionForm: FormGroup;
formSubmitted = false; //Stores form status
private nameValidators = [
Validators.minLength(1),
Validators.maxLength(50)
];
private emailValidators = [
Validators.maxLength(250),
Validators.email
];
constructor(private fb: FormBuilder) { }
ngOnInit(): void {
this.createForm();
}
createForm(): void {
this.submissionForm = this.fb.group({
anonymous: [''],
name: ['', this.nameValidators],
contact: [''],
email: ['', this.emailValidators],
contentWarning: ['', [Validators.required]],
title: ['', Validators.required],
message: ['', [Validators.required, Validators.minLength(10), Validators.maxLength(10000)],
agree: [false, [Validators.requiredTrue]]
});
}
onSubmit() {
if (this.submissionForm.get('anonymous').value == false) {
this.submissionForm.get('name').setValidators(this.nameValidators.concat(Validators.required));
} else {
this.submissionForm.get('name').setValidators(this.nameValidators);
}
if (this.submissionForm.get('contact').value == true) {
this.submissionForm.get('email').setValidators(this.emailValidators.concat(Validators.required));
} else {
this.submissionForm.get('email').setValidators(this.emailValidators);
}
this.formSubmitted = true; //The form has been submitted
console.log(this.submissionForm.valid);
if (this.submissionForm.valid) {
console.log('submissionForm', this.submissionForm.value); //Proceed with form processing
}
}
get anonymous() { return this.submissionForm.get('anonymous') };
get name() { return this.submissionForm.get('name') };
get contact() { return this.submissionForm.get('contact') };
get email() { return this.submissionForm.get('email') };
get contentWarning() { return this.submissionForm.get('contentWarning') };
get title() { return this.submissionForm.get('title') };
get message() { return this.submissionForm.get('message') };
get agree() { return this.submissionForm.get('agree') };
}
Below is the relevant template code:
<section class="section">
<div class="columns is-centered">
<div class="column is-four-fifths">
<h1 class="title">Submission Form</h1>
<p class="success" *ngIf="formSubmitted">Successfully Submitted.</p>
<form [formGroup]="submissionForm" (ngSubmit)="onSubmit()">
<div class="field">
<!--Anonymous Checkbox-->
<label class="checkbox">
<input type="checkbox"
formControlName="anonymous">
Remain anonymous
</label>
</div>
<div class="field" [hidden]="anonymous.value">
<!--Anonymous Name Field-->
<input class="input"
type="text"
placeholder="Name"
formControlName="name"
[ngClass]="{'form-submitted': formSubmitted}">
<div class="help-is-danger" *ngIf="name.invalid && !name.pristine">Maximum 50 characters allowed for the name field.</div>
</div>
<div class="field">
<!--Contact Checkbox-->
<label class="checkbox">
<input type="checkbox" formControlName="contact">
Contact me
</label>
</div>
<div class="field" [hidden]="!contact.value">
<!--Email Field-->
<input class="input"
type="text"
placeholder="Email"
formControlName="email"
[ngClass]="{'form-submitted': formSubmitted}">
<div class="help-is-danger" *ngIf="email.invalid && !email.pristine">
<p *ngIf="email.errors.email">A valid email address is required.</p>
</div>
</div>
<div class="field">
<!--Content Warning Field-->
<input class="input"
type="text"
placeholder="Content Warnings"
formControlName="name"
[ngClass]="{'form-submitted': formSubmitted}">
<div class="help-is-danger" *ngIf="contentWarning.invalid && !contentWarning.pristine">Please provide content warnings.</div>
</div>
<div class="field">
<!--Title Field-->
<input class="input"
type="text"
placeholder="Title"
formControlName="title"
[ngClass]="{'form-submitted': formSubmitted}">
<div class="help-is-danger" *ngIf="title.invalid && !title.pristine">Title is a mandatory field.</div>
</div>
<div class="field">
<!--Message Text Area-->
<textarea class="textarea"
type="text"
placeholder="Your Submission"
formControlName="message">
</textarea>
<div class="help-is-danger" *ngIf="message.invalid && !message.pristine">
<p *ngIf="message.errors.minlength">Message should contain at least 10 characters.</p>
</div>
</div>
<div class="field">
<!--'Agree' Checkbox-->
<label class="checkbox">
<input type="checkbox"
formControlName="agree">
Agree
</label>
<div class="help-is-danger" *ngIf="submissionForm.hasError('required', 'agree') && !agree.pristine">You must check 'Agree' to proceed with the submission.</div>
</div>
<!--Submit Button-->
<button type="submit" class="button is-danger is-outlined" [disabled]="!agree.valid && !submissionForm.valid">Submit</button>
</form>
</div>
</div>
</section>
This application uses Angular 9 along with the Bulma framework.