themisir / form-validator

Simplest form validation for flutter form widgets
https://pub.dev/packages/form_validator
MIT License
78 stars 41 forks source link

Double can be null but less then some double value in TextFormField. #31

Open mjarpitanand opened 2 years ago

mjarpitanand commented 2 years ago

I have a textformfield in which i want user can leave that i.e not required field but if it fill should be less than some double value. I have keyboard formattor to set to digits only.

themisir commented 2 years ago

You can provide custom validators using .add method like:

ValidationBuilder(optional: true)
  ..add((value) => (double.tryParse(value) ?? 0.0) < someDoubleValue
    ? null
    : 'Value has to be less than $someDoubleValue');

Callbacks provided to add method is same as validator callbacks for TextFormField which means you return null if validation passes or return error message.