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
When i am selecting from the browser selection it is not working
Though i focused out of the input field its still in uppercase.
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
2nd edit:
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
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
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With