valform
boilerplate-free form validation library.
why?
there is no clean and nice way to separate business logic (validation) from presentation.
why not formz?
formz defies flutter’s way to handle state, forcing user to hold form state entirely in business logic.
but it can’t be done in flutter. state is inevitably stored in texteditingcontrollers. formatters are applied inside widget itself.
when i use formz, i always find myself copy-pasting form inputs and struggling with simple use cases.
why valform?
- minimalistic.
- perfectly fits flutter’s form handling system.
- your teammates just need to explore a couple of simple concepts.
simple usage
use case: we want to validate that email doesn’t exist in our database. if it does, display error.
for this case i will use vfreproduce
, because i want to keep an error, if user accidentally enters previously validated email.
class loginformstate extends changenotifier {
vfreproduce _emailalreadyexists;
loginformstate([
this._emailalreadyexists = const vfreproduce.sealed()
]);
string? validateemail(string? email) {
if (_emailalreadyexists.access(email)) {
return "email already exists";
}
return null;
}
future<void> submit() {
await future.delayed(duration(seconds: 1)); // access the database
_emailalreadyexists = vfreproduce();
notifylisteners();
}
}
somewhere in flutter code:
final loginformstate = loginformstate();
void initstate() {
super.initstate();
loginformstate.addlistener(() => setstate(() {}));
}
widget build(buildcontext context) => column(
children: [
textformfield(
validator: loginformstate.validateemail,
),
outlinedbutton(onpressed: loginformstate.submit),
]
);
inspiration
the whole work was inspired by event concept in async_redux, created by marcelo glasberg.
Comments are closed.