v0.8.0
formz
Dart에서 폼 표현과 검증을 일반적인 방식으로 간소화하는 것을 목표로 하는 통합 폼 표현입니다.
915좋아요 17.7만30일 다운로드160Pub 점수
AndroidiOSWebmacOSWindowsLinux
Very Good Ventures 🦄 에서 사용하는 Dart 의 통합 폼 표현
^1.16.0^1.26.3^11.0.0아래는 영문 원문 스냅샷입니다. 최신 내용은 GitHub에서 확인하세요.
Very Good Ventures Very Good Ventures
Developed with 💙 by Very Good Ventures 🦄
ci coverage pub package License: MIT style: very good analysis
A unified form representation in Dart. Formz aims to simplify form representation and validation in a generic way.
import 'package:formz/formz.dart';
// Define input validation errors
enum NameInputError { empty }
// Extend FormzInput and provide the input type and error type.
class NameInput extends FormzInput<String, NameInputError> {
// Call super.pure to represent an unmodified form input.
const NameInput.pure() : super.pure('');
// Call super.dirty to represent a modified form input.
const NameInput.dirty({String value = ''}) : super.dirty(value);
// Override validator to handle validating a given input value.
@override
NameInputError? validator(String value) {
return value.isEmpty ? NameInputError.empty : null;
}
}
const name = NameInput.pure();
print(name.value); // ''
print(name.isValid); // false
print(name.error); // NameInputError.empty
print(name.displayError); // null
const joe = NameInput.dirty(value: 'joe');
print(joe.value); // 'joe'
print(joe.isValid); // true
print(joe.error); // null
print(name.displayError); // null
const validInputs = <FormzInput>[
NameInput.dirty(value: 'jan'),
NameInput.dirty(value: 'jen'),
NameInput.dirty(value: 'joe'),
];
print(Formz.validate(validInputs)); // true
const invalidInputs = <FormzInput>[
NameInput.dirty(),
NameInput.dirty(),
NameInput.dirty(),
];
print(Formz.validate(invalidInputs)); // false
class LoginForm with FormzMixin {
LoginForm({
this.username = const Username.pure(),
this.password = const Password.pure(),
});
final Username username;
final Password password;
@override
List<FormzInput> get inputs => [username, password];
}
void main() {
print(LoginForm().isValid); // false
}
For cases where the validator method has an expensive implementation, consider using the FormzInputErrorCacheMixin mixin to cache the error result and improve performance.
import 'package:formz/formz.dart';
enum EmailValidationError { invalid }
class Email extends FormzInput<String, EmailValidationError>
with FormzInputErrorCacheMixin {
Email.pure([super.value = '']) : super.pure();
Email.dirty([super.value = '']) : super.dirty();
static final _emailRegExp = RegExp(
r'^[a-zA-Z\d.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z\d-]+(?:\.[a-zA-Z\d-]+)*$',
);
@override
EmailValidationError? validator(String value) {
return _emailRegExp.hasMatch(value) ? null : EmailValidationError.invalid;
}
}