2

I'm trying to use flutter_form_builder: ^7.1.0 with form_builder_validators to create a form, but a getting undefined errors at

onChanged: _onChanged, and validator: FormBuilderValidators.compose.

I tried following the example at flutter_form_builder but the example seems to be either incomplete or not up to date. When I try to add autovalidate: true, I get another undefined error. How can I fix this?

class _CreateCompanyViewState extends State<CreateCompanyView> {

  @override
  void initState() {
    super.initState();
  }

  final _formKey = GlobalKey<FormBuilderState>();


  @override
  Widget build(BuildContext context) {

 FormBuilder(
                 
                key: _formKey,

                child: Column(
          
                  children: <Widget>[

                    FormBuilderTextField(

                      name: 'age',          

                      decoration: InputDecoration(
                     
                      labelText: 'This value is passed along to the [Text.maxLines] attribute of the [Text] widget used to display the hint text.',
                      
                      ),
              
                      onChanged: _onChanged,
            
                        validator: FormBuilderValidators.compose([
               
                          FormBuilderValidators.required(context),

                          FormBuilderValidators.numeric(context),
                        
                          FormBuilderValidators.max(context, 70),
               
                        ]),
              
                          keyboardType: TextInputType.number,
            
                        ),

                  Row(

                    children: <Widget>[
        
                      Expanded(
            
                        child: MaterialButton(
              
                          color: Theme.of(context).colorScheme.secondary,
              
                        child: Text("Submit", style: TextStyle(color: Colors.white),),

                          onPressed: () {
                
                            _formKey.currentState?.save();
                
                            if (_formKey.currentState!.validate()) {
                  
                              print(_formKey.currentState!.value);
                
                            } else {
                  
                              print("validation failed");
                            }
                          },
                        ),
                      ),
          
                      SizedBox(width: 20),
          
                      Expanded(
            
                        child: MaterialButton(

                          color: Theme.of(context).colorScheme.secondary,
              
                          child: Text("Reset", style: TextStyle(color: Colors.white),),
              
                          onPressed: () {

                            _formKey.currentState!.reset();
                          },
                        ),
                      ),
                    ],
                  )
                ],
              )
            ),
            ],
          ),
        ),
      ),
    )));
  }
0

2 Answers 2

1

FormBuilderValidators is not handling null value. You need to check null value, then perform others validators.

validator: FormBuilderValidators.compose([
  (val) {
    return val == null ? "Field is empty" : null;
  },
  FormBuilderValidators.required(context), 
/// others validator
]),
Sign up to request clarification or add additional context in comments.

2 Comments

This made no difference, the undefined errors remained.
Can you recheck the parenthesis, try flutter clean and restarting the IDE. Also make sure to add form_builder_validators package.
0

If you are getting issues with form_builder_validators of lately, it means you are using a new version form_builder_validators that still have bugs.

What to do, you have to revers your flutter version and download flutter sdk 3.7.12 on the link below 3.7.12 DOWNLOAD Flutter SDK - V 3.7.12

THEN Downgrade your packages too to

  flutter_form_builder: ^7.7.0
  form_builder_validators: ^8.3.0

At least I am sure those two work properly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.