Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do i need dirty and pure using formz package in Dart?

Tags:

flutter

dart

I don't really get the concept of the formz package in dart.

According to https://pub.dev/packages/formz

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;
  }
}

There is a dirty and a pure representation of a form input, but why do i need this?

Isn't it just about validating the form input via the overwritten validator method? Therefore it would be enough to just have one constructor and run the validation?

like image 748
harrow Avatar asked Sep 15 '25 09:09

harrow


1 Answers

I think i can answer it by myself. Dirty or clean form elements can be used as an indicator to decide for example if a save button is enabled or disabled in a form.

Imagine the following situation.

First Name -> John -> pure
Last Name -> Doe -> pure
Age -> 32 -> pure

And then check with

Formz.isPure([firstName, lastname, age])

will return true and the save button should be disabled

Now, the age is changed to 33

First Name -> John -> pure
Last Name -> Doe -> pure
Age -> 33 -> dirty

And

Formz.isPure([firstName, lastname, age])

will be false and the button should be enabled

like image 91
harrow Avatar answered Sep 17 '25 01:09

harrow