Skip to content

Commit cdaadf3

Browse files
committed
Formatters & Validators #13
1 parent 383f280 commit cdaadf3

File tree

6 files changed

+58
-24
lines changed

6 files changed

+58
-24
lines changed

lib/src/api/models/text_input_formatter_model.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ sealed class TextInputFormatterModel with EquatableMixin, SerializableMixin {
3232
final TextInputFormatterType type =
3333
TextInputFormatterType.values.byName(json['type']);
3434
return switch (type) {
35-
TextInputFormatterType.none => NoneTextInputFormatter.fromJson(json),
35+
TextInputFormatterType.none => NoneTextInputFormatterModel.fromJson(json),
3636
TextInputFormatterType.regex =>
3737
RegexTextInputFormatterModel.fromJson(json),
3838
};
3939
}
4040

4141
/// A list of all available text field formatters.
4242
static const List<TextInputFormatterModel> formatters = [
43-
NoneTextInputFormatter(),
43+
NoneTextInputFormatterModel(),
4444
...RegexTextInputFormatterModel.formatters,
4545
];
4646

@@ -55,17 +55,17 @@ sealed class TextInputFormatterModel with EquatableMixin, SerializableMixin {
5555

5656
/// A formatter than does not restrict the input in any way.
5757
@JsonSerializable()
58-
class NoneTextInputFormatter extends TextInputFormatterModel {
59-
/// Creates a new [NoneTextInputFormatter] instance.
60-
const NoneTextInputFormatter()
58+
class NoneTextInputFormatterModel extends TextInputFormatterModel {
59+
/// Creates a new [NoneTextInputFormatterModel] instance.
60+
const NoneTextInputFormatterModel()
6161
: super(name: 'None', type: TextInputFormatterType.none);
6262

6363
/// Creates a [TextInputFormatterModel] instance from a JSON object.
64-
factory NoneTextInputFormatter.fromJson(Map<String, dynamic> json) =>
65-
_$NoneTextInputFormatterFromJson(json);
64+
factory NoneTextInputFormatterModel.fromJson(Map<String, dynamic> json) =>
65+
_$NoneTextInputFormatterModelFromJson(json);
6666

6767
@override
68-
Map toJson() => _$NoneTextInputFormatterToJson(this);
68+
Map toJson() => _$NoneTextInputFormatterModelToJson(this);
6969
}
7070

7171
/// Text formatters that can be applied to the text field input to restrict the

lib/src/api/models/text_input_formatter_model.g.dart

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/api/models/text_input_validator_model.dart

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,17 +177,33 @@ sealed class ConfigurableTextInputValidatorModel
177177

178178
/// Represents absence of any validation.
179179
@JsonSerializable()
180-
class RequiredTextInputValidatorModel extends TextInputValidatorModel {
180+
class RequiredTextInputValidatorModel
181+
extends ConfigurableTextInputValidatorModel {
181182
/// Creates a new [RequiredTextInputValidatorModel] instance.
182-
const RequiredTextInputValidatorModel()
183-
: super(name: 'Required', type: TextInputValidatorType.required);
183+
const RequiredTextInputValidatorModel({
184+
super.errorMessage = 'This field is required.',
185+
}) : super(
186+
name: 'Required',
187+
type: TextInputValidatorType.required,
188+
required: true,
189+
);
184190

185191
@override
186192
String? validate(String? input) {
187-
if (input == null || input.isEmpty) return 'This field is required.';
193+
if (input == null || input.isEmpty) return errorMessage;
188194
return null;
189195
}
190196

197+
@override
198+
RequiredTextInputValidatorModel copyWith({
199+
bool? required,
200+
String? errorMessage,
201+
}) {
202+
return RequiredTextInputValidatorModel(
203+
errorMessage: errorMessage ?? this.errorMessage,
204+
);
205+
}
206+
191207
/// Creates a [TextInputValidatorModel] instance from a JSON object.
192208
factory RequiredTextInputValidatorModel.fromJson(Map<String, dynamic> json) =>
193209
_$RequiredTextInputValidatorModelFromJson(json);

lib/src/api/models/text_input_validator_model.g.dart

Lines changed: 23 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/api/nodes/text_field_node.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ class TextFieldProperties with SerializableMixin, EquatableMixin {
245245
this.expands = false,
246246
this.showDecimalKey = false,
247247
this.showSignKey = false,
248-
this.formatter = const NoneTextInputFormatter(),
248+
this.formatter = const NoneTextInputFormatterModel(),
249249
this.validator = const NoneTextInputValidatorModel(),
250250
this.autovalidateMode = AutovalidateModeC.onUserInteraction,
251251
this.autofillHints = const {},

lib/src/api/nodes/text_field_node.g.dart

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)