oneOf static method
Creates a validator that checks if the control's value is one of the values
in the provided collection.
For String values, the comparison can be made case-sensitive or insensitive
using the caseSensitive parameter. For other types, this parameter is ignored.
Example:
Validates that the country is one of the allowed countries (case-sensitive).
final countryControl = FormControl<String>(
validators: [Validators.oneOf(['USA', 'Canada', 'Mexico'])],
);
countryControl.value = 'USA';
print(countryControl.valid); // true
countryControl.value = 'usa';
print(countryControl.valid); // false (due to case sensitivity)
Example:
Validates that the fruit is one of the allowed fruits (case-insensitive).
final fruitControl = FormControl<String>(
validators: [
Validators.oneOf(
['Apple', 'Banana', 'Orange'],
caseSensitive: false,
),
],
);
fruitControl.value = 'apple';
print(fruitControl.valid); // true
fruitControl.value = 'Grape';
print(fruitControl.valid); // false
Example:
Validates that the number is one of the allowed numbers.
final numberControl = FormControl<int>(
validators: [Validators.oneOf([1, 2, 3, 42])],
);
numberControl.value = 42;
print(numberControl.valid); // true
numberControl.value = 5;
print(numberControl.valid); // false
Implementation
static Validator<dynamic> oneOf(
List<dynamic> collection, {
bool caseSensitive = true,
}) {
return OneOfValidator(collection, caseSensitive: caseSensitive);
}