Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Pipe not working when selecting the browser suggestion for input field

I am using the built-in pipe titlecase on an input field - username in a reactive form. It is working properly only when i am typing in the input field and its not working when i select from the browser suggestion for that input field, even when i focused out.

app.component.ts

ngOnInit() {
    this.signupForm = new FormGroup({
      'userData': new FormGroup({
        'username': new FormControl(null, [Validators.required, this.forbiddenNames.bind(this)]),
        'email': new FormControl('[email protected]', [Validators.required, Validators.email], this.forbiddenEmails)
      }),
      'gender': new FormControl('male'),
      'hobbies': new FormArray([])
    });
}

app.component.html

<form [formGroup]="signupForm" (ngSubmit)="onSubmit()">
    <div formGroupName="userData">
        <div class="form-group">
           <label for="username">Username</label>
             <input
                  type="text"
                  id="username"
                  formControlName="username"
                  class="form-control"
                  [value]="signupForm.get('userData.username').value | titlecase">
                  <span *ngIf="signupForm.get('userData.username').errors['required']">
                      This field is required
                  </span>
          </div>
         ...
     </div>
     <button class="btn btn-primary" type="submit">Submit</button>
 </form>

When i am typing it is working fine

enter image description here

When i am selecting from the browser selection it is not working
Though i focused out of the input field its still in uppercase.

enter image description here Can someone help on what i am doing wrong.


@Haifeng Zhang This is the scenario i mentioned in the question, i replicated in your stackblitz demo

enter image description here

like image 586
Avinash Avatar asked Jul 11 '18 20:07

Avinash


2 Answers

2nd edit: enter image description here Check the very last action in the GIF, I type in space and ADD transforms to Add

That's how titlecase work, the definition is:

Transforms text to title case. Capitalizes the first letter of each word, and transforms the rest of the word to lower case. Words are delimited by any whitespace character, such as a space, tab, or line-feed character.

1st edit:

I use tempref and it works for me: Demo is here

enter image description here

app.component.html:

<form [formGroup]="signUpForm">
  <div formGroupName="userData">
    <label for="username">Username</label>
    <input type="text" formControlName="username" #username [value]="username.value | titlecase">
  </div>
  <pre>{{ signUpForm.value | json }}</pre>
</form>

app.component.ts:

 constructor(public fb: FormBuilder) {
    this.signUpForm = this.fb.group({
      userData: this.fb.group({
        username: '',
        email: ''
      })
    });
  }

It also works when I copy a uppercase 'ABC' into the input field

enter image description here enter image description here enter image description here

like image 137
Haifeng Zhang Avatar answered Nov 17 '22 22:11

Haifeng Zhang


I used another approach which is subscribing to valuechange

see live demo here at stackblitz

constructor(private fb: FormBuilder, private titleCasePipe: TitleCasePipe) {
  }

  ngOnInit() {
    this.signupForm = this.fb.group({
      'userData': this.fb.group({
        'username': [null, [Validators.required]],
        'email': ['[email protected]', [Validators.required, Validators.email]]
      }),
      'gender': ['male'],
      'hobbies': this.fb.array([])
    });

    this.signupForm.controls.userData.get('username').valueChanges.subscribe((val) => {
      this.signupForm.controls.userData.patchValue(
        {
          username: this.titleCasePipe.transform(val)
        }, 
        { emitEvent: false })
    })
  }

and

<form [formGroup]="signupForm" (ngSubmit)="onSubmit()">
    <div formGroupName="userData">
        <div class="form-group">
           <label for="username">Username</label>
             <input
                  type="text"
                  id="username"
                  formControlName="username"
                  class="form-control"
                  >
                  <span *ngIf="signupForm.get('userData.username').errors && signupForm.get('userData.username').errors['required']">
                      This field is required
                  </span>
          </div>
     </div>
     <button class="btn btn-primary" type="submit">Submit</button>
 </form>

result

like image 39
Reza Avatar answered Nov 17 '22 20:11

Reza