Starts in:
1 DAY
22 HRS
55 MIN
38 SEC
Starts in:
1 D
22 H
55 M
38 S
Open Closed

How can I force show validation errors in an angular UI? #1806


User avatar
0
GregB created
  • ABP Framework version: v4.3.3
  • UI type: Angular
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): no
  • Exception message and stack trace: N/A
  • Steps to reproduce the issue:"

How can I force all validation errors to show in an Angular form?

I've got a simple form with a required field

 this.form = this.fb.group({
            name: [name ?? null, [Validators.required]]
           });

But when I click submit, no errors are shown.

submitForm()
{
   if(!this.form.valid)
   {
       // How do I show the errors?
       return;
   }
   
   // ...
}

If I type something into the field, then delete it, the required validation message shows.

I want to show all validation errors when the user tries to submit an invalid form.


1 Answer(s)
  • User Avatar
    1
    Mehmet created

    Hello @GregB

    There are two ways to achieve this:

    • You should mark form controls as dirty as shown below:
    if(!this.form.valid)
    {
       Object.values(this.form.controls).forEach((control) => {
            control.markAsDirty();
            control.updateValueAndValidity();
        });
       return;
    }
    
    • You can use the ngx-validate's valiteOnSubmit directive. See the working example: https://stackblitz.com/edit/ngx-validate?file=src/app/components/app.component.html
Made with ❤️ on ABP v9.1.0-preview. Updated on November 20, 2024, 13:06