themisir / form-validator

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

Is there any way to get validation as bool? #18

Closed 2shrestha22 closed 3 years ago

2shrestha22 commented 3 years ago

Is it possible to get bool also from validation result? I need to validate form in logic also not only in UI so if there is something like ValidationBuilder().email().isValid('my@email.com') is will be nice. Can this be achieved using extension?

themisir commented 3 years ago

Yes of course.

ValidationBuilde()
  .email()
  .test('my@email.com')
2shrestha22 commented 3 years ago

I think it returns String, isn't it?

themisir commented 3 years ago

I think it returns String, isn't it?

If the validation passed it will return null otherwise the error message will be returned. So you can check if value equals to null or not like that:

if (ValidationBuilde().email().test('my@email.com') == null) {
  // validation passed
}

Or:

final result = ValidationBuilde().email().test('my@email.com');

if (result == null) {
  // Validation passed
} else {
  print('Validation error: $result');
}
2shrestha22 commented 3 years ago

Okay, thank you for the solution. Btw it will be a nice feature to add.