fix(#156): valider téléphone au blur et NIR en live.
Formatage NIR progressif + contrôles au fil de la saisie ; téléphone validé à la perte de focus comme e-mail / CP. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -49,12 +49,21 @@ String nirToRaw(String normalized) {
|
||||
return s;
|
||||
}
|
||||
|
||||
/// Formate pour affichage : 1 12 34 56 789 012 - 34 ou 1 12 34 2A 789 012 - 34 (Corse).
|
||||
/// Formate pour affichage (complet ou en cours de saisie) :
|
||||
/// `1 12 34 56 789 012 - 34` ou `1 12 34 2A 789 012 - 34` (Corse).
|
||||
String formatNir(String raw) {
|
||||
final r = nirToRaw(raw);
|
||||
if (r.length < 15) return r;
|
||||
// Même structure pour tous : sexe + année + mois + département + commune + ordre-clé.
|
||||
return '${r.substring(0, 1)} ${r.substring(1, 3)} ${r.substring(3, 5)} ${r.substring(5, 7)} ${r.substring(7, 10)} ${r.substring(10, 13)} - ${r.substring(13, 15)}';
|
||||
final r = nirToRaw(raw).toUpperCase();
|
||||
if (r.isEmpty) return '';
|
||||
final buf = StringBuffer();
|
||||
for (var i = 0; i < r.length && i < 15; i++) {
|
||||
if (i == 1 || i == 3 || i == 5 || i == 7 || i == 10) {
|
||||
buf.write(' ');
|
||||
} else if (i == 13) {
|
||||
buf.write(' - ');
|
||||
}
|
||||
buf.write(r[i]);
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
/// Aligné sur le backend (NIR côté API / nir.util.ts) : sexe 1–3, département 2A ou 2B pour la Corse.
|
||||
@@ -92,20 +101,39 @@ String? validateNir(String? value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Formateur de saisie : affiche le NIR formaté (1 12 34 56 789 012 - 34) et limite à 15 caractères utiles.
|
||||
/// Validation pendant la saisie : pas d’erreur si incomplet encore plausible ;
|
||||
/// dès 15 caractères, même contrôles que [validateNir].
|
||||
String? validateNirTyping(String? value) {
|
||||
if (value == null || value.trim().isEmpty) return null;
|
||||
final raw = nirToRaw(value).toUpperCase();
|
||||
if (raw.isEmpty) return null;
|
||||
if (raw[0] != '1' && raw[0] != '2' && raw[0] != '3') {
|
||||
return 'Format NIR invalide (ex. 1 12 34 56 789 012 - 34 ou 2A pour la Corse)';
|
||||
}
|
||||
if (raw.length < 15) return null;
|
||||
return validateNir(value);
|
||||
}
|
||||
|
||||
/// Formateur de saisie : affiche le NIR formaté au fil de la frappe et limite à 15 caractères utiles.
|
||||
class NirInputFormatter extends TextInputFormatter {
|
||||
const NirInputFormatter();
|
||||
|
||||
@override
|
||||
TextEditingValue formatEditUpdate(
|
||||
TextEditingValue oldValue,
|
||||
TextEditingValue newValue,
|
||||
) {
|
||||
final raw = normalizeNir(newValue.text);
|
||||
if (raw.isEmpty) return newValue;
|
||||
if (raw.isEmpty) {
|
||||
return const TextEditingValue(
|
||||
text: '',
|
||||
selection: TextSelection.collapsed(offset: 0),
|
||||
);
|
||||
}
|
||||
final formatted = formatNir(raw);
|
||||
final offset = formatted.length;
|
||||
return TextEditingValue(
|
||||
text: formatted,
|
||||
selection: TextSelection.collapsed(offset: offset),
|
||||
selection: TextSelection.collapsed(offset: formatted.length),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user