-
-
Notifications
You must be signed in to change notification settings - Fork 44
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
Comments
Hello @yehiaazanki
|
hmm. ok |
@lcuis I think to accomplish this you need to change SearchChoices to extend 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);
}
);
} |
Thanks a lot @martin-labanic for your explanation and code. |
+1 |
…n-labanic #68 * Recording Flutster automated integration testing as a playable video.
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? |
@lcuis Apologies for the late reply. Unfortunately it does not work as expected as the failed validation does not get accounted for when a |
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. |
…rent form validator. Thanks @martin-labanic #68
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 If and when you can, I would love to get your opinion on this functionality @martin-labanic |
+1 |
Why validator don`t respect formkey??
The text was updated successfully, but these errors were encountered: