-1
I want to display the error message when the user changes or makes the input active using Pattern [ X20-xff]{5,50}$
But when I type characters that do not answer the field, it only turns red and the error message does not appear.
<form [formGroup]="ChannelCreateForm" (ngSubmit)="CreateChannel()">
<mat-form-field class="input-full-width" appearance="standard">
<mat-label>{{'channel.NewChannelPage.name' | transloco}}</mat-label>
<input matInput type="text" formControlName="ChannelTitle">
<mat-hint align="start" *ngIf="ChannelTitle.errors?.pattern && (ChannelTitle.dirty || ChannelTitle.touched)" class="text-danger">inválido</mat-hint>
<mat-hint align="end">{{titleInput.value.length}} / 50</mat-hint>
</mat-form-field>
....
</form>
Pattern50Char = "^[\x20-\xFF]{5,50}$";
ChannelCreateForm: FormGroup;
constructor(
public _FormBuilder: FormBuilder,
public _MatSnackBar: MatSnackBar
) {
this.ChannelCreateForm = this._FormBuilder.group({
ChannelTitle: ['', [Validators.pattern(this.Pattern50Char), Validators.maxLength(50)]]
});
}
get titleInput() {
return this.ChannelCreateForm.get('ChannelTitle');
}
I made the changes and updated the post with the information you posted, but the error message still does not appear, the field turns red when you lose Focus.. but no message.
– RRV
@RRV in your example you are referencing the
channelTitle
in the*ngIf
, alteration totitleInput
and tell me– Patrick Lima
Patrick.. Thank you, problem solved.
– RRV
Patrick.. Taking advantage can make a doubt.. When I start the group, considering a data update form, how do I keep the information that is in the input? for example do not delete channelTitle channelTitle: [" ", [Validators.Pattern(this.myPattern), Validators.maxlength(50)]]
– RRV
@RRV do not know if I understand your question, you wanted to do a reset of information?
– Patrick Lima
I was referring to starting fields with values recovered from BD. I did so -> this.UserNameForm.get('Username'). setValue(data.user_name) and it worked.
– RRV