import 'package:flutter/services.dart'; /// Longueur maximale d’une adresse e-mail (RFC 5321). const int kEmailMaxLength = 254; /// Trim + minuscules (usage à la perte de focus et avant validation côté API). String normalizeEmailText(String raw) { return raw.trim().toLowerCase(); } /// Motif volontairement simple pour l’UI (pas une validation RFC complète). /// Local + @ + domaine avec au moins un point ; pas d’espaces. final RegExp kAppEmailPattern = RegExp( r'^[a-zA-Z0-9._%+\-]+@([a-zA-Z0-9\-]+\.)+[a-zA-Z]{2,63}$', ); /// Indique si [trimmed] est un e-mail plausible (insensible à la casse). bool isValidEmailFormat(String trimmed) { final s = normalizeEmailText(trimmed); if (s.isEmpty || s.length > kEmailMaxLength) { return false; } return kAppEmailPattern.hasMatch(s); } /// Validation centralisée pour les champs e-mail. /// /// Si [allowEmpty] est `true`, une chaîne vide (après trim) est acceptée. String? validateEmail(String? raw, {bool allowEmpty = false}) { final s = normalizeEmailText(raw ?? ''); if (s.isEmpty) { return allowEmpty ? null : 'L’adresse e-mail est obligatoire.'; } if (s.length > kEmailMaxLength) { return 'L’adresse e-mail est trop longue ($kEmailMaxLength caractères maximum).'; } if (!kAppEmailPattern.hasMatch(s)) { return 'Le format de l’adresse e-mail est incorrect.'; } return null; } /// Limite la longueur saisie dans un champ e-mail ([kEmailMaxLength]). class EmailMaxLengthFormatter extends TextInputFormatter { const EmailMaxLengthFormatter(); @override TextEditingValue formatEditUpdate( TextEditingValue oldValue, TextEditingValue newValue, ) { if (newValue.text.length <= kEmailMaxLength) { return newValue; } return oldValue; } }