formatEditUpdate method

  1. @override
TextEditingValue formatEditUpdate(
  1. TextEditingValue oldValue,
  2. TextEditingValue newValue
)
override

Called when text is being typed or cut/copy/pasted in the EditableText.

You can override the resulting text based on the previous text value and the incoming new text value.

When formatters are chained, oldValue reflects the initial value of TextEditingValue at the beginning of the chain.

Implementation

@override
TextEditingValue formatEditUpdate(
    TextEditingValue oldValue, TextEditingValue newValue) {
  final text = newValue.text;

  // Enforce max length

  if (maxLength !=  null && text.length > maxLength!) {
    pShowToast(message: message??'Password length should be between $minLength to $maxLength');
    return oldValue; // Revert if length exceeds max
  }

  // Check if input meets complexity requirements
  if (isComplex) {
    final complexPattern = regExp??RegExp(r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[A-Za-z\d]*$'); // At least 1 uppercase, 1 lowercase, 1 digit
    if (!complexPattern.hasMatch(text) && text.isNotEmpty) {
      if(showToast??true){
        pShowToast(message:  message??"Password must include at least 1 uppercase, 1 lowercase, and 1 number");
      }
      return oldValue; // Revert if input doesn't match complex rules
    }
  }

  // Enforce min length only when submitting (use external validation in form)
  if (maxLength !=  null && text.length < minLength! && text.isNotEmpty) {
    pShowToast(message: message??'Password length should be between $minLength to $maxLength');
    return newValue; // Allow user to input until it matches min length
  }

  return newValue;
}