flutter-form-builder-ecosystem / flutter_form_builder

Simple form maker for Flutter Framework
https://pub.dev/packages/flutter_form_builder
MIT License
1.48k stars 535 forks source link

dynamically compose validators list #1053

Closed fehernyul closed 2 years ago

fehernyul commented 2 years ago

Environment

Package name:
flutter_form_builder: ^7.3.1 form_builder_validators: ^8.1.1

Hi there,

Is it possible that case, I have some FormBuilderTextField and I need to validate them with FormFieldValidator lists. I want to get the validators from a function. Some FormBuilderTextField is String type and some are integer, double,, date, ...

This is the code which unfortunately doesn't work:

Form:

           FormBuilder(
              key: _formKey,
              autovalidateMode: AutovalidateMode.disabled,
              child: Column(
                children: <Widget>[
                  FormBuilderTextField(
                    name: 'required_number',
                    validator: getValidators(isInteger: true, requiredValue: true),
                  ),

                  FormBuilderTextField(
                    name: 'required_string',
                    validator: getValidators(isInteger: false, requiredValue: true),
                  ),

                  FormBuilderTextField(
                    name: 'non_required_number',
                    validator: getValidators(isInteger: true, requiredValue: false),
                  ),

                  FormBuilderTextField(
                    name: 'non_required_string',
                    validator: getValidators(isInteger: false, requiredValue: false),
                  ),
                ],
              ),
            )

The getValidators method:

  String? Function(dynamic) getValidators({required bool requiredValue, required bool isInteger}) {
    List<FormFieldValidator> result = <FormFieldValidator>[];

    if (requiredValue) {
      // it's works
      result.add(
        FormBuilderValidators.required(),
      );

      // if this is a number, then must fill with minium: 1
      if (isInteger) {
        result.add(
          FormBuilderValidators.min(1),
        );
      }
    }

    if (isInteger) {
      // it's NOT works
      result.add(
        FormBuilderValidators.numeric(), // ==> The argument type 'String? Function(String?)' can't be assigned to the parameter type 'String? Function(dynamic)'
      );
      result.add(
        FormBuilderValidators.integer(),// ==> The argument type 'String? Function(String?)' can't be assigned to the parameter type 'String? Function(dynamic)'
      );
    }

    return FormBuilderValidators.compose(result);
  }

Can you help me to solve this "getValidators" method please?

It could be the best if I will can add some custom validator too. (related to https://github.com/danvick/flutter_form_builder/issues/1002)

Thank you, Gabor

deandreamatias commented 2 years ago

Hi! Validators.compose don't work to this case?

      validator: FormBuilderValidators.compose(
        [
          FormBuilderValidators.email(
            context,
          ),
          FormBuilderValidators.required(
            context,
          ),
        ],
      ),

Or create a custom validator, like:

  static FormFieldValidator<String> iban({
    required String ibanErrorText,
  }) =>
      (valueCandidate) {
        if (valueCandidate != null && valueCandidate.isNotEmpty) {
          return functionToValidateIban(valueCandidate) ? null : ibanErrorText;
        }
        return null;
      };
fehernyul commented 2 years ago

Hi,

Yes it works this way, but I need to use via a method. I need to the validator returned from a function. This is a very simple code, I have a bunch of FormBuilderTextField which is generated by a json file. The form is fully generated by the json's content. I don't know in advance what kind of validator it will need. That is what I have to use a function which result a validator list by some extra parameter. I must use a function, and I put in that what I found in the json. minLength, maxLength, minValue, maxValue, minDateTime, maxDateTime, required, ...

Can you help me to write this function? (like in the example I showed)

Thank you!

deandreamatias commented 2 years ago

You can create a dynamic validators with FormBuilderValidators.compose

  validator: FormBuilderValidators.compose(
    [
     if(isInteger)
      FormBuilderValidators.minLength(
        context,
      ),
     if(String)
      FormBuilderValidators.required(
        context,
      ),
    ],
  ),
fehernyul commented 2 years ago

This is works very fine! Thank you!! :)

ojolmotovlog commented 1 year ago

hallo, can i make a function using await? because my function return Future, in FormBuilderValidators.compose?

fehernyul commented 1 year ago

hallo, can i make a function using await? because my function return Future, in FormBuilderValidators.compose?

It's a good idea, but this issue was closed. I think you have to create a new one with this question.