You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi @shariful2011,
That is the correct use of validators within a FormBuilderCustomField. Also note that when you use validators property, then you don't need to use validator property in your embedded FormField because it will be redundant.
I'd also suggest that you do some reading on the documentation of FormField because I noticed that you may have some issues in your code.
Consider calling field.didChange(val); within your onChanged function of the dropdown as that will notify the FormField that the value for the input has changed.
Note that initialValue in FormField is set once. Changing _districtVal will not change the initialValue.
How can I validate FormBuilderCustomField? I added
validators: [
FormBuilderValidators.required(
errorText: 'Please select District.'),
],
but it's not validating. Can please help?
Below is my code:
return FormBuilderCustomField(
attribute: 'district',
validators: [
FormBuilderValidators.required(
errorText: 'Please select District.'),
],
formField: FormField(
autovalidate: true,
initialValue: _districtVal,
validator: (String val) {
if (val.length == 0) {
return 'Please enter district';
}
return null;
},
enabled: true,
builder: (FormFieldState field) {
return InputDecorator(
decoration: InputDecoration(
labelText: "Select district",
contentPadding: EdgeInsets.only(top: 10.0, bottom: 0.0),
border: InputBorder.none,
labelStyle: TextStyle(
fontFamily: 'Open Sans',
fontSize: 19,
),
),
child: DropdownButton(
isExpanded: true,
items: _districtList,
value: _districtVal,
onChanged: (dynamic val) {
print('selected district=$val');
widget.model.selectedDistrict = int.parse(val);
setState(() {
_districtVal = val;
_districtList.clear();
});
},
),
);
},
),
);
The text was updated successfully, but these errors were encountered: