0
I have a group of static messages for form field validation. These messages are present throughout the application and so I would like to use some way to make them dynamic and organized within the structure.
Today I have organized group within my . ts that way for example:
login ts.
validation_messages = {
'email': [
{ type: 'required', message: 'O email precisa ser preenchido' },
],
'password': [
{ type: 'required', message: 'A senha precisa ser preenchida' },
{ type: 'minlength', message: 'O campo de senha precisa ter no mínimo 8 digitos' },
{ type: 'maxlength', message: 'O campo de senha precisa ter no máximo 16 digitos' },
]};
login.html
<form novalidate [formGroup]="formLogin">
<ion-item>
<label>Email</label>
<ion-input type="email" placeholder="Digite seu email" [(ngModel)]="userModel.email" formControlName="email"></ion-input>
</ion-item>
<ng-container *ngFor="let validation of validation_messages.email">
<div *ngIf="formLogin.controls['email'].hasError(validation.type) && formLogin.controls['email'].dirty && formLogin.controls['email'].touched">
{{validation.message}}
</div>
</ng-container>
<ion-item>
<label>Password</label>
<ion-input type="password" placeholder="Digite sua senha" [(ngModel)]="userModel.password" formControlName="password"></ion-input>
</ion-item>
<ng-container *ngFor="let validation of validation_messages.password">
<div *ngIf="formLogin.controls['password'].hasError(validation.type) && formLogin.controls['password'].dirty && formLogin.controls['password'].touched">
{{validation.message}}
</div>
</ng-container>
<div class="form-group">
<button class="btn btn-primary btn-lg active" (click)="doLogin()" [disabled]="!formLogin.valid">Entrar</button>
</div>
</form>
Is there any way to organize these static messages with ENUM, or is there some other way with "pipe, Component" ?
Guy is not recommended to have an architecture this way separated by the file type because it is not very scalable. Imagine when you have 100 components inside /components. you would have a module for everyone? It’s not a good one. A more complete answer here https://answall.com/questions/302874/howto understand a-architecture-mvc-no-angular-4/303068#303068
– Eduardo Vargas
Cool Eduardo! In fact you may not have understood my real point, your article statement is correct and one of the ways to create a project structure yes. There is a structure in which I find incredible and attend me whenever it is based on this concept here : https://github.com/Robinyo/ionic-angular-schematics ! In this example up there my focus was only to have a repository for static messages and not the structuring of Components...
– douglas baptista