我想将从密码控制器获得的字符串数据发送到自定义验证器生成器。当我测试它时,我注意到文本编辑控制器中的字符串数据无法到达验证生成器。但当我尝试用按钮访问它时,文本编辑控制器工作正常。
我知道Flutter中没有Validation Builder类。但我已经习惯了这个包裹。
我的一些代码在这里
child: TextFormField(
controller: _repeatPassController,
validator:
ValidationBuilder(requiredMessage: "Bu alan gereklidir")
.repeatPassword(passController.text)
.build(),
obscureText: true,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Tekrar Şifre',
hintText: 'Güvenli bir şifre giriniz'),
),
我的这部分代码保存在另一个名为consts.dart.的文件中
import 'package:form_validator/form_validator.dart';
class Consts {
static final emailValidator = ValidationBuilder(
requiredMessage: "Bu alan gereklidir",
).email("E-mail geçerli değil!").build();
static final passValidator =
ValidationBuilder(requiredMessage: "Bu alan gereklidir!")
.password()
.build();
}
extension CustomValidationBuilder on ValidationBuilder {
password() => add((value) {
if (value == null || value.isEmpty) {
return "Şifre gereklidir";
}
if (value.length < 5) {
return 'Şifreniz en az 5 karakter olmalıdır!';
}
return null;
});
}
extension CustomValidationBuilderRepeatPassword on ValidationBuilder {
///check if password repeated
///if password is not repeated return error message
repeatPassword(String password) => add((value) {
if (value == null || value.isEmpty) {
return "Bu alan gereklidir";
}
if (value != password) {
print(value + " value"); ///out = "myvalue value"
print(password + " password"); /// out=" password" is printing null
return 'Şifreler eşleşmiyor!';
}
return null;
});
}
我想我知道发生了什么。字符串是不可变的对象。因此,当您执行repeatPassword(controller.value(时,它会传递实际值,而不是引用该值,这意味着它将根据初始值(可能是空字符串(检查密码。
修复传递TextEdgintController的问题,改为:
extension CustomValidationBuilderRepeatPassword on ValidationBuilder {
///check if password repeated
///if password is not repeated return error message
repeatPassword(TextEditingController password) => add((value) {
if (value == null || value.isEmpty) {
return "Bu alan gereklidir";
}
if (value != password.value) {
return 'Şifreler eşleşmiyor!';
}
return null;
});
}
感谢您对";themisir";来自表单验证器社区。