How to sanitize input values before validation?

Hi, I like to implement a sanitizer for e.g. removing heading and trailing spaces from an input string. I have 2 questions about that:

  1. is it possible to implement the sanitization just before validation?
  2. is it possible to set the value of the text field inside the onChange method without using a controller? I’m new to Dart and I see I can’t access properties of TextFormField by using this?

_

TextFormField(
      decoration: InputDecoration(
        labelText: 'Email*', hintText: "john.doe@gmail.com"),
      keyboardType: TextInputType.emailAddress,
      validator: emailValidator,
      onChanged: (String str) {
        // ?
      }
    ),

Thank you!

2 Likes
emailValidator (String value) {
            String sanitizedVal = value.trim();
            if (sanitizedVal.isEmpty) {
              return 'Email is required';
            }
            return null;
          }
TextFormField(
          decoration: InputDecoration(
            labelStyle: TextStyle(color: Colors.grey),
            labelText: 'Email *',
            contentPadding: EdgeInsets.all(0.0),
          ),
          validator: (String value) {
            String sanitizedVal = value.trim();
            if (sanitizedVal.isEmpty) {
              return 'Email is required';
            }
            return null;
          },
          keyboardType: TextInputType.text,
          onChanged: (value) {
            email = value;
          },
          onSaved: (String value) {
            email = value;
          },
        )