簡體   English   中英

單獨的 Dart 文件中的表單驗證器

[英]Form validators in a separate Dart file

我正在考慮在單獨的 dart 文件中編寫驗證器,以便獲得更清晰的代碼。 我確實寫了一個新的dart文件並寫了一個帶有條件的字符串方法但是如何在textformfield的validator參數中初始化dart文件?

TextFormField(
       textCapitalization: TextCapitalization.characters,
       controller: _articleNumberController,
       validator: Validators.validateName(),
       decoration: InputDecoration(
            prefixIcon: Icon(Icons.straighten),
            labelStyle: Theme.of(context).textTheme.subhead,
            labelText: 'Article Number',
            contentPadding: EdgeInsets.all(15.0),
            isDense: true,
            border: OutlineInputBorder(
                   borderRadius: BorderRadius.circular(4.0),
                   ),
                ),
   ),

這是驗證器的單獨 class 文件

class Validators {
  String validateName(String value) {
    String pattern = r'(^[a-zA-Z ]*$)';
    RegExp regExp = new RegExp(pattern);
    if (value.length == 0) {
      return "Name is Required";
    } else if (!regExp.hasMatch(value)) {
      return "Name must be a-z and A-Z";
    }
    return null;
  }

}

如果我嘗試在驗證器中將驗證器初始化為validator: Validators.validateName()我收到錯誤,因為The argument type 'String' can't be assigned to the parameter type 'String Function(String)'

我認為您需要在初始化方法時添加static

class Validators {
 static String validateName(String value) {
    String patttern = r'(^[a-zA-Z ]*$)';
    RegExp regExp = new RegExp(patttern);
    if (value.length == 0) {
      return "Name is Required";
    } else if (!regExp.hasMatch(value)) {
      return "Name must be a-z and A-Z";
    }
    return null;
  }
}

然后你的代碼看起來像

TextFormField(
       textCapitalization: TextCapitalization.characters,
       controller: _articleNumberController,
       validator: Validators.validateName,
       decoration: InputDecoration(
            prefixIcon: Icon(Icons.straighten),
            labelStyle: Theme.of(context).textTheme.subhead,
            labelText: 'Article Number',
            contentPadding: EdgeInsets.all(15.0),
            isDense: true,
            border: OutlineInputBorder(
                   borderRadius: BorderRadius.circular(4.0),
                   ),
                ),
   ),

我希望這個方法對你有用

TextFormField(
 textCapitalization: TextCapitalization.characters,
 controller: _articleNumberController,
 validator: (value) => Validators.validateName(value),
 decoration: InputDecoration(
 prefixIcon: Icon(Icons.straighten),
 labelStyle: Theme.of(context).textTheme.subhead,
 labelText: 'Article Number',
 contentPadding: EdgeInsets.all(15.0),
 isDense: true,
 border: OutlineInputBorder(
    borderRadius: BorderRadius.circular(4.0),
   ),
  ),
),

/// class

class Validators {
  static String validateName(String value) {
    String patttern = r'(^[a-zA-Z ]*$)';
    RegExp regExp = new RegExp(patttern);
    if (value.length == 0) {
      return "Name is Required";
    } else if (!regExp.hasMatch(value)) {
      return "Name must be a-z and A-Z";
    }
    return null;
  }

}

試試這個 package https://pub.dev/packages/queen_validators

import 'package/queen_validators/queen_validators.dart';
@override
Widget build(BuildContext context) {
return TextFormField(
     // use qValidator function and provider list of rules to apply on this field
    validator: qValidator([
      IsRequired(),
      IsEmail(),
      MinLength(8),
      MaxLength(30, "optionally you can override the failure if the validation fails"),
    ]),
  );
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM