Skip to content

Instantly share code, notes, and snippets.

@Klerith
Created January 27, 2025 18:39
Show Gist options
  • Save Klerith/1d99f88d7740158ad48b87b3747cd151 to your computer and use it in GitHub Desktop.
Save Klerith/1d99f88d7740158ad48b87b3747cd151 to your computer and use it in GitHub Desktop.
Utilidades para formularios reactivos
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 '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 === '[email protected]') {
return {
emailTaken: true,
};
}
return null;
}
static notStrider(control: AbstractControl): ValidationErrors | null {
const value = control.value;
return value === 'strider' ? { noStrider: true } : null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment