themisir / form-validator

Simplest form validation for flutter form widgets
https://pub.dev/packages/form_validator
MIT License
78 stars 41 forks source link

Passing `ValidationBuilder()` to validator has this error #37

Open anburocky3 opened 2 years ago

anburocky3 commented 2 years ago

Passing ValidationBuilder() to validator has this error: image

This is my code:

  TextField(
                  controller: passwordController,
                  labelText: 'Password',
                  hintText: 'Password',
                  obscureText: true,
                  validator: ValidationBuilder().required().minLength(8).build(),
                ),

And I'm my custom TextField() is

class TextField extends StatelessWidget {
  final String labelText;
  final String hintText;
  final bool? obscureText;
  final TextEditingController controller;
  final FormFieldValidator? validator;

  const TextField({
    Key? key,
    required this.labelText,
    required this.hintText,
    this.obscureText,
    required this.controller,
    this.validator,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return TextFormField(
      controller: controller,
      textInputAction: TextInputAction.next,
      decoration: InputDecoration(
        border: const OutlineInputBorder(),
        hintText: hintText,
        labelText: labelText,
      ),
      obscureText: obscureText ?? false,
      validator: validator,
    );
  }
}
anburocky3 commented 2 years ago

@themisir If i pass this

final FormFieldValidator? validator;

with this, It works.

final dynamic validator;
ariefsn commented 2 years ago

perhaps can change the type to final FormFieldValidator<String>? validator;

Miguel-A-Jara commented 8 months ago

@themisir If i pass this

final FormFieldValidator? validator;

with this, It works.

final dynamic validator;

Note that in every example given for this library, the .build() method is always called at the end of a validation chain. This method has the following shape:

String? Function(String?) build()

which is the same shape as the TextFormField's validator parameter.

You should change your prop from:

final FormFieldValidator? validator;

to:

final String? Function(String?)? validator;