-
-
Save farney9/e7cb13cf3361bc4778d2a3cfa2e15ee0 to your computer and use it in GitHub Desktop.
Utilidades para formularios reactivos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { | |
| AbstractControl, | |
| FormArray, | |
| FormGroup, | |
| ValidationErrors, | |
| } from '@angular/forms'; | |
| async function sleep() { | |
| return new Promise((resolve) => { | |
| setTimeout(() => { | |
| resolve(true); | |
| }, 2500); | |
| }); | |
| } | |
| export class FormUtils { | |
| // Expresiones regulares | |
| static namePattern = '([a-zA-Z]+) ([a-zA-Z]+)'; | |
| static emailPattern = '^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,4}$'; | |
| static notOnlySpacesPattern = '^[a-zA-Z0-9]+$'; | |
| static slugPattern = '^[a-z0-9_]+(?:-[a-z0-9_]+)*$'; | |
| static getTextError(errors: ValidationErrors) { | |
| for (const key of Object.keys(errors)) { | |
| switch (key) { | |
| case 'required': | |
| return 'Este campo es requerido'; | |
| case 'minlength': | |
| return `Mínimo de ${errors['minlength'].requiredLength} caracteres.`; | |
| case 'min': | |
| return `Valor mínimo de ${errors['min'].min}`; | |
| case 'email': | |
| return `El valor ingresado no es un correo electrónico`; | |
| case 'emailTaken': | |
| return `El correo electrónico ya está siendo usado por otro usuario`; | |
| case 'noStrider': | |
| return `No se puede usar el username de strider en la app`; | |
| case 'noOnlyWhitespace': | |
| return 'Este campo no puede contener solo espacios en blanco'; | |
| case 'pattern': | |
| if (errors['pattern'].requiredPattern === FormUtils.emailPattern) { | |
| return 'El valor ingresado no luce como un correo electrónico'; | |
| } | |
| return 'Error de patrón contra expresión regular'; | |
| default: | |
| return `Error de validación no controlado ${key}`; | |
| } | |
| } | |
| return null; | |
| } | |
| static isValidField(form: FormGroup, fieldName: string): boolean | null { | |
| return ( | |
| !!form.controls[fieldName].errors && form.controls[fieldName].touched | |
| ); | |
| } | |
| static getFieldError(form: FormGroup, fieldName: string): string | null { | |
| if (!form.controls[fieldName]) return null; | |
| const errors = form.controls[fieldName].errors ?? {}; | |
| return FormUtils.getTextError(errors); | |
| } | |
| static isValidFieldInArray(formArray: FormArray, index: number) { | |
| return ( | |
| formArray.controls[index].errors && formArray.controls[index].touched | |
| ); | |
| } | |
| static getFieldErrorInArray( | |
| formArray: FormArray, | |
| index: number | |
| ): string | null { | |
| if (formArray.controls.length === 0) return null; | |
| const errors = formArray.controls[index].errors ?? {}; | |
| return FormUtils.getTextError(errors); | |
| } | |
| static isFieldOneEqualFieldTwo(field1: string, field2: string) { | |
| return (formGroup: AbstractControl) => { | |
| const field1Value = formGroup.get(field1)?.value; | |
| const field2Value = formGroup.get(field2)?.value; | |
| return field1Value === field2Value ? null : { passwordsNotEqual: true }; | |
| }; | |
| } | |
| static async checkingServerResponse( | |
| control: AbstractControl | |
| ): Promise<ValidationErrors | null> { | |
| console.log('Validando contra servidor'); | |
| await sleep(); // 2 segundos y medio | |
| const formValue = control.value; | |
| if (formValue === 'hola@mundo.com') { | |
| return { | |
| emailTaken: true, | |
| }; | |
| } | |
| return null; | |
| } | |
| static notStrider(control: AbstractControl): ValidationErrors | null { | |
| const value = control.value; | |
| return value === 'strider' ? { noStrider: true } : null; | |
| } | |
| // Validador que rechaza campos que solo contienen espacios en blanco | |
| static noOnlyWhitespace(control: AbstractControl): ValidationErrors | null { | |
| const value = control.value; | |
| // Si el valor es null, undefined o vacío, lo maneja Validators.required | |
| if (!value) { | |
| return null; | |
| } | |
| // Si después de quitar espacios en blanco queda vacío, es inválido | |
| if (typeof value === 'string' && value.trim().length === 0) { | |
| return { noOnlyWhitespace: true }; | |
| } | |
| return null; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment