File tree Expand file tree Collapse file tree 2 files changed +62
-0
lines changed Expand file tree Collapse file tree 2 files changed +62
-0
lines changed Original file line number Diff line number Diff line change @@ -172,6 +172,48 @@ class FormBuilderValidators {
172
172
};
173
173
}
174
174
175
+ /// [FormFieldValidator] that requires the words count of the field's value to be
176
+ /// greater than or equal to the provided minimum count.
177
+ static FormFieldValidator <String > minWordsCount (
178
+ int minCount, {
179
+ bool allowEmpty = false ,
180
+ String ? errorText,
181
+ }) {
182
+ assert (minCount > 0 );
183
+ return (valueCandidate) {
184
+ int valueWordsCount = 0 ;
185
+
186
+ if (valueCandidate != null ) {
187
+ if (valueCandidate.isEmpty) {
188
+ valueWordsCount = 0 ;
189
+ } else {
190
+ valueWordsCount = valueCandidate.split (' ' ).length;
191
+ }
192
+ }
193
+
194
+ return valueWordsCount < minCount && (! allowEmpty || valueWordsCount > 0 )
195
+ ? errorText ??
196
+ FormBuilderLocalizations .current.minWordsCountErrorText (minCount)
197
+ : null ;
198
+ };
199
+ }
200
+
201
+ /// [FormFieldValidator] that requires the words count of the field's value to be
202
+ /// less than or equal to the provided maximum count.
203
+ static FormFieldValidator <String > maxWordsCount (
204
+ int maxCount, {
205
+ String ? errorText,
206
+ }) {
207
+ assert (maxCount > 0 );
208
+ return (valueCandidate) {
209
+ int valueWordsCount = valueCandidate? .split (' ' ).length ?? 0 ;
210
+ return null != valueCandidate && valueWordsCount > maxCount
211
+ ? errorText ??
212
+ FormBuilderLocalizations .current.maxWordsCountErrorText (maxCount)
213
+ : null ;
214
+ };
215
+ }
216
+
175
217
/// [FormFieldValidator] that requires the field's value to be a valid email address.
176
218
static FormFieldValidator <String > email ({
177
219
String ? errorText,
You can’t perform that action at this time.
0 commit comments