Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
60
rated 0 times [  61] [ 1]  / answers: 1 / hits: 22248  / 8 Years ago, wed, december 7, 2016, 12:00:00

I'm trying to create a contact form. The form looks like this:



<form novalidate [formGroup]=contact (ngSubmit)=send()>
<p>
<label>Name
<br>
<input type=text class=input value= formControlName=name>
<span class=error>Enter your name</span>
</label>
</p>
<p>
<label>E-mail
<br>
<input type=email class=input value= formControlName=email>
<span class=error>It looks like this email is invalid</span>
</label>
</p>
<p>
<label>Phone
<br>
<input type=text class=input value= formControlName=telefone>
<span class=error>It looks like this phone number is invalid</span>
</label>
</p>
<p>
<label>Message
<br>
<textarea type=text class=input value= formControlName=message></textarea>
<span class=error>The message can't be empty</span>
</label>
</p>
<p class=submit>
<input type=submit name=submit class=bt value=Send>
</p>
</form>


In this form only the message and the name and email or the phone number fields should be required.



I'm using a formBuilder class, so here's the TypeScript code:



this.contact = this.formBuilder.group({
name: ['', Validators.required],
email: ['', Validators.compose([/*Use custom validador??*/])],
phone: ['', Validators.compose([/*Use custom validador??*/]],
message: ['', Validators.required]
});


I tried using custom validators as a solution, but I could not figure out a solution. Any suggestions?


More From » html

 Answers
10

Yes, a custom validator is the way to go.



Make your form group like this:



this.contact = this.formBuilder.group({
name: ['', Validators.required],
email: ['', Validators.required],
phone: ['', Validators.required],
message: ['', Validators.required]
}, {validator: this.customValidationFunction})


Then have the customValidationFunction check for validation. Made up validation just for example:



customValidationFunction(formGroup): any {
let nameField = formGroup.controls['name'].value; //access any of your form fields like this
return (nameField.length < 5) ? { nameLengthFive: true } : null;
}


Change each input like this (changing your p tags to divs. Substitute the control name for each and change syntax for the hidden span tag validation where appropriate):



<div [ngClass]={'has-error':!contact.controls['name'].valid && contact.controls['name'].touched}>
<label>Name</label>
<input class=input type=text [formControl]=contact.controls['name']>
<span [hidden]=!contact.hasError('nameLengthFive') class=error>Enter your name</span>
</div>

[#59786] Monday, December 5, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
quentinaveryb

Total Points: 102
Total Questions: 100
Total Answers: 93

Location: Colombia
Member since Mon, May 2, 2022
2 Years ago
quentinaveryb questions
Thu, Aug 6, 20, 00:00, 4 Years ago
Fri, Jul 17, 20, 00:00, 4 Years ago
Mon, Aug 12, 19, 00:00, 5 Years ago
;