11import '../../localization/l10n.dart' ;
22import '../constants.dart' ;
33
4- /// This function returns a transformer validator that checks if the user input
5- /// is neither null nor empty, in the case it is a collection, a string or a map.
6- /// If the condition was not satisfied, it returns the `isRequiredMsg` error message,
7- /// if provided, or ´FormBuilderLocalizations.current.requiredErrorText´ otherwise.
8- /// If the condition is satisfied, it returns the validation of the input with
9- /// `v` or null if `v` was not provided.
4+ /// This function generates a validator that enforces a required field rule. If
5+ /// the input is not provided (i.e., null or empty), the function returns an
6+ /// error message indicating that the field is required. If the input is
7+ /// provided, the function applies an additional validator v (if supplied) to
8+ /// further validate the input.
109Validator <T ?> isRequired <T extends Object >([
1110 Validator <T >? v,
1211 String ? isRequiredMsg,
@@ -24,10 +23,19 @@ Validator<T?> isRequired<T extends Object>([
2423 return finalValidator;
2524}
2625
27- Validator <T ?> isOptional <T extends Object >(
28- Validator <T >? v, {
29- String ? isOptionalMsg,
30- }) {
26+ String errorIsOptionalTemporary (String vErrorMessage) {
27+ return 'The field is optional, otherwise, $vErrorMessage ' ;
28+ }
29+
30+ /// This function generates a validator that marks a field as optional. If the
31+ /// user input is not provided (i.e., it's null or empty), the validator will
32+ /// return null, indicating no validation error. If the input is provided, the
33+ /// function applies an additional validator v (if provided) to further validate
34+ /// the input.
35+ Validator <T ?> isOptional <T extends Object >([
36+ Validator <T >? v,
37+ String Function (String )? isOptionalMsg,
38+ ]) {
3139 String ? finalValidator (T ? value) {
3240 final (bool isValid, T ? transformedValue) =
3341 _isRequiredValidateAndConvert (value);
@@ -40,7 +48,8 @@ Validator<T?> isOptional<T extends Object>(
4048 return null ;
4149 }
4250
43- return 'The field is optional, otherwise, $vErrorMessage ' ;
51+ return isOptionalMsg? .call (vErrorMessage) ??
52+ errorIsOptionalTemporary (vErrorMessage);
4453 }
4554
4655 return finalValidator;
0 commit comments