Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validator #68

Open
yehiaazanki opened this issue Feb 16, 2022 · 10 comments
Open

Validator #68

yehiaazanki opened this issue Feb 16, 2022 · 10 comments
Labels
enhancement New feature or request waiting for customer response The team needs more information from the user

Comments

@yehiaazanki
Copy link

Why validator don`t respect formkey??

@lcuis
Copy link
Owner

lcuis commented Feb 16, 2022

Hello @yehiaazanki

search_choices is not supporting the standard Flutter forms features at the moment. However, this would be a great adaptation. Do you have any idea how this could be achieved?

@yehiaazanki
Copy link
Author

Hello @yehiaazanki

search_choices is not supporting the standard Flutter forms features at the moment. However, this would be a great adaptation. Do you have any idea how this could be achieved?

hmm. ok

@martin-labanic
Copy link

martin-labanic commented Jun 29, 2022

@lcuis I think to accomplish this you need to change SearchChoices to extend FormField instead of StatefulWidget to get access to flutters form validation.

This a wrapper class I wrote as a workaround to get form validation working but its far from ideal.

import 'package:flutter/material.dart';
import 'package:search_choices/search_choices.dart';

class TTSearchChoices extends FormField<dynamic> {
  dynamic current_value;
  final ValueSetter<String>? on_changed;
  List<DropdownMenuItem<dynamic>>? items;
  bool is_expanded;
  bool display_clear_icon;
  bool auto_focus_search;
  Function? search_function;
  
  static String? _defaultValidator(dynamic v) {
    return null;
  }

  static void _defaultOnSaved(dynamic v) {
    // Currently does nothing, not sure if this is the correct way to do this but it appears to be working without it.
  }

  TTSearchChoices(
    this.current_value,
    this.on_changed,
    this.items,
    this.is_expanded,
    this.display_clear_icon,
    this.auto_focus_search,
    this.search_function,
    { FormFieldSetter<dynamic> on_saved = _defaultOnSaved,
      FormFieldValidator<dynamic> validator = _defaultValidator,
      AutovalidateMode autovalidate_mode = AutovalidateMode.onUserInteraction,
    }
  ): super(
    onSaved: on_saved,
    validator: validator,
    initialValue: current_value,
    autovalidateMode: autovalidate_mode,
    builder: (FormFieldState<dynamic> state) {
      
      void onChangedHandler(dynamic value) {
        state.didChange(value);
        if (on_changed != null) {
          on_changed(value as String);
        }
      }

      Widget result = SearchChoices.single(
        isExpanded: is_expanded,
        value: current_value,
        onChanged: onChangedHandler,
        items: items,
        displayClearIcon: display_clear_icon,
        padding: 0,
        autofocus: auto_focus_search,
        searchFn: search_function,
      );

      if (!state.isValid && state.errorText != null && state.errorText!.isNotEmpty) {
        result = Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            result,
            Text(
              state.errorText ?? '',
              style: TextStyle(
                color: Colors.red[700],
                fontSize: 13.0,
              ),
            ),
          ]
        );
      }

      return Align(alignment: Alignment.topLeft, child: result);
    }
  );

}

@lcuis
Copy link
Owner

lcuis commented Jun 29, 2022

Thanks a lot @martin-labanic for your explanation and code.
I am looking forward to digging into this possibility.

@lcuis lcuis reopened this Jun 29, 2022
@lcuis lcuis added the enhancement New feature or request label Jul 7, 2022
@caniko
Copy link

caniko commented Sep 27, 2022

+1

lcuis added a commit that referenced this issue Sep 29, 2022
…n-labanic #68 * Recording Flutster automated integration testing as a playable video.
@lcuis
Copy link
Owner

lcuis commented Sep 30, 2022

Hello @martin-labanic ,

Version 2.1.2 contains an initial effort to adapt search_choices to extend FormField.

Can you please let me know whether this is what you expected?

@martin-labanic
Copy link

martin-labanic commented Oct 28, 2022

@lcuis Apologies for the late reply.

Unfortunately it does not work as expected as the failed validation does not get accounted for when a Form widgets validate function is called.

@lcuis
Copy link
Owner

lcuis commented Oct 28, 2022

Hi @martin-labanic !

No worries for the delay, I understand very well the time it takes to get back to such questions.

Thanks a lot for your answer!

Indeed, I was able to create a form with a validation that doesn't take into account the SearchChoices validator result.

I will try to understand why and how to change this behavior.

lcuis added a commit that referenced this issue Oct 28, 2022
@lcuis
Copy link
Owner

lcuis commented Oct 28, 2022

It looks like version 2.1.5 is at least a partial solution to this request.

The new example "Validator in form" shows a form with single and multiple search_choices Widgets. If at least one of those widgets is not valid, the form is not valid either.

However, running the automated integration testing with Flutster made me realize that there are some specific situations where the call to didChange - necessary to update the value of the form field object and thus letting the validation work from a form perspective - fails when in multiple-future cases. I am not sure whether it is possible to overcome this constraint.

If and when you can, I would love to get your opinion on this functionality @martin-labanic

@Macacoazul01 Macacoazul01 added the waiting for customer response The team needs more information from the user label Dec 14, 2022
@lucasgm1997
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request waiting for customer response The team needs more information from the user
Projects
None yet
Development

No branches or pull requests

6 participants