1

我正在尝试flutter_form_builder: ^7.1.0form_builder_validators一起使用来创建表单,但出现undefined错误

onChanged: _onChanged,validator: FormBuilderValidators.compose

我尝试按照flutter_form_builder 的示例进行操作,但该示例似乎不完整或不是最新的。当我尝试添加时,autovalidate: true,我得到另一个未定义的错误。我怎样才能解决这个问题?

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

1 回答 1

1

FormBuilderValidators不处理空值。您需要检查空值,然后执行其他验证器。

validator: FormBuilderValidators.compose([
  (val) {
    return val == null ? "Field is empty" : null;
  },
  FormBuilderValidators.required(context), 
/// others validator
]),
于 2022-02-13T12:54:25.833 回答