4

I have one reactive form validator sample. it shows validation error message initially before we enter value.

expected behavior is, after enter value it has to show error message

sample link

4
  • 1
    Why check dirty and touched? Commented Oct 9, 2018 at 5:41
  • No need to use bold for your whole question Commented Oct 9, 2018 at 15:45
  • okay. @JamesZ thanks for your update Commented Oct 10, 2018 at 9:23
  • Thanks @Vikas. i got your response Commented Oct 10, 2018 at 9:24

2 Answers 2

8

You have to check touched for your error message like this

 <div *ngIf="check.errors.required && check.touched" class="e-error">
                This field is required.
 </div>

You are checking required so simply when you load the form then your field is obviously empty so it will throw the error.

You will get more information and example here : Built In Validators and Reactive Form Validattions

Sign up to request clarification or add additional context in comments.

Comments

3

Create a Form Service and Use Mark FormGroup Touched so that it will defaultly don't display the Error Messages

import { FormService } from './services/form';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.scss' ]
})

export class AppComponent  {
 public testForm: FormGroup;

ngOnInIt(){
 this.testForm.valueChanges.subscribe((data) => {
    this.FormService.markFormGroupTouched(this.testForm);
})
}
}

3 Comments

Hi Balaji, thanks for your support.... did you check this with my sample...but it is not working
can you please , apply your idea into that sample.?
@kumaresan_sd sure will apply this to your solution, please wait

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.