title: Why validator is showing error for same field in edit mode in angular reactive form?
body: <p>I have a requirement to have a unique name field. I am using same form for user registration and edit mode.</p>
  <p>My validation logic sends request to backend and brings all the usernames available and store them in an array list. And when user enter username
  the entered username is compared with the existing usernames from database. If found, validation comes in and validation error is displayed
  like &quot;Username already exists!!!&quot;</p>
  <p>My user form is as below:</p>
  <pre><code>  createUserForm() {
      this.userRegistrationform = this.fb.group(
        {
          id: [null],
          name: [null, Validators.required, CustomValidators.userNameValidator(this.userService)], //username validator placed
        }
      );
    }
  </code></pre>
  <p>These are code in CustomValidators class:</p>
  <pre><code>static existingUserNames: [];
    constructor(private http: HttpClient) {
      this.storeUserNames();
    }
  
  
   checkIfUserNameExists(value: string) {
      return of(UserService.existingUserNames.some((name) =&gt; name === value)).pipe(
        delay(1000)
      );
    }
  
    public storeUserNames() {
      this.getAllUserNames().subscribe((data) =&gt; {
        UserService.existingClientNames = data;
      })
    }
    
  //async validator to validate if username already exists
   static userNameValidator(userService: UserService): AsyncValidatorFn {
          return (control: AbstractControl): Observable&lt;ValidationErrors&gt; =&gt; {
              return userService
                  .checkIfUserNameExists(control.value)
                  .pipe(
                      map((result: boolean) =&gt;
                          result ? { userNameAlreadyExists: true } : null
                      )
                  );
          };
      }
  </code></pre>
  <p>Below is how I have caught error in html file:</p>
  <pre><code> &lt;span *ngIf=&quot;form.name.hasError('userNameAlreadyExists')&quot;&gt;
         User name already exists!!!
   &lt;/span&gt;
  </code></pre>
  <p>Problems:</p>
  <ol>
  <li><p>If I enter username in user register mode like &quot;name : John + space&quot; and add space after name it works. So why it is working only after I add space in name value in user register mode?</p>
  </li>
  <li><p>As I am using same form for edit mode as well, username field shows error &quot;User name already exists!!!&quot; error and invalidates whole form directly if I open form in edit mode. Why is this happening?</p>
  </li>
  </ol>
  <p>Any kind of suggestion or solution would be great. Thank You.</p>