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

How to validate FormBuilderCustomField? #64

Closed
shariful2011 opened this issue May 22, 2019 · 2 comments
Closed

How to validate FormBuilderCustomField? #64

shariful2011 opened this issue May 22, 2019 · 2 comments

Comments

@shariful2011
Copy link

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();
});
},
),
);
},
),
);

@danvick
Copy link
Collaborator

danvick commented May 23, 2019

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.

@danvick
Copy link
Collaborator

danvick commented May 23, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants