flutter-form-builder-ecosystem / flutter-form-builder-ecosystem/form_builder_validators

SelectLengthValidator

Open
#144 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Dart
Stars
67
Forks
106
Avg merge
2d 21h
Merged PRs (30d)
1

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Package/Plugin version

11.1.1

### What you'd like to happen

Hello. I would like to suggest that you implement a length validator, with options to choose from. This may be necessary for some standardized form values, such as INN (which can be either 10 or 12 characters long).

### Alternatives you've considered

Not found

### Aditional information

```
class SelectLengthValidator extends TranslatedValidator {
final Set lengthOptions;

const SelectLengthValidator(this.lengthOptions, {
super.errorText,
super.checkNullOrEmpty,
});

@override
String get translatedErrorText => 'The length of the value must be one of the following values: $lengthOptions.';

@override
String? validateValue(T valueCandidate) {
final valueLength = switch (valueCandidate) {
String() => valueCandidate.length,
Iterable() => valueCandidate.length,
Map() => valueCandidate.length,
_ => 0,
};

return !lengthOptions.contains(valueLength) ? errorText : null;
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.