Skip to content

Commit 78e238c

Browse files
committed
Formatters & Validators #1
- [Exp] models and transformer linking.
1 parent 479f3bd commit 78e238c

13 files changed

+291
-51
lines changed

lib/src/api/models/models.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ export 'size.dart';
5656
export 'start_end_prop.dart';
5757
export 'text_baseline.dart';
5858
export 'text_direction.dart';
59+
export 'text_input_formatter_model.dart';
60+
export 'text_input_validator_model.dart';
5961
export 'text_input_type.dart';
6062
export 'text_overflow.dart';
6163
export 'trigger.dart';
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import 'package:codelessly_json_annotation/codelessly_json_annotation.dart';
2+
import 'package:equatable/equatable.dart';
3+
4+
import '../mixins.dart';
5+
6+
part 'text_input_formatter_model.g.dart';
7+
8+
/// Text formatters that can be applied to the text field input to restrict the
9+
/// input to a specific format.
10+
@JsonSerializable()
11+
class TextInputFormatterModel with EquatableMixin, SerializableMixin {
12+
/// The regular expression to match the text.
13+
final String pattern;
14+
15+
/// The name of the formatter.
16+
final String name;
17+
18+
/// Allow only digits in the text field.
19+
static const TextInputFormatterModel none = TextInputFormatterModel(
20+
name: 'None',
21+
pattern: r'.*',
22+
);
23+
24+
/// Allow only digits in the text field.
25+
static const TextInputFormatterModel digitsOnly = TextInputFormatterModel(
26+
name: 'Digits Only',
27+
pattern: r'[0-9]',
28+
);
29+
30+
/// Allow only alphabets in the text field.
31+
static const TextInputFormatterModel alphabetsOnly =
32+
TextInputFormatterModel(name: 'Alphabets Only', pattern: r'[a-zA-Z]');
33+
34+
/// Allow only alpha-numeric characters in the text field.
35+
static const TextInputFormatterModel alphaNumeric =
36+
TextInputFormatterModel(name: 'Alpha-numeric', pattern: r'[a-zA-Z0-9]');
37+
38+
/// Allow only phone number format in the text field.
39+
static const TextInputFormatterModel noSpaces =
40+
TextInputFormatterModel(name: 'No Spaces', pattern: r'^[^\s]*$');
41+
42+
/// Allow only email format in the text field.
43+
static const TextInputFormatterModel email =
44+
TextInputFormatterModel(name: 'Email', pattern: '^(|\\S)+\$');
45+
46+
/// Allow only phone number format in the text field.
47+
static const TextInputFormatterModel phoneNumber =
48+
TextInputFormatterModel(name: 'Phone Number', pattern: r'^+?[0-9]*$');
49+
50+
/// List of all available text field formatters.
51+
static const List<TextInputFormatterModel> values = [
52+
none,
53+
digitsOnly,
54+
alphabetsOnly,
55+
alphaNumeric,
56+
noSpaces,
57+
email,
58+
phoneNumber,
59+
];
60+
61+
/// Returns true if the formatter is [none].
62+
bool get isNone => this == none;
63+
64+
/// Creates a new [TextInputFormatterModel] instance with the given pattern.
65+
const TextInputFormatterModel({required this.name, required this.pattern});
66+
67+
/// copyWith
68+
TextInputFormatterModel copyWith({
69+
String? name,
70+
String? pattern,
71+
}) {
72+
return TextInputFormatterModel(
73+
name: name ?? this.name,
74+
pattern: pattern ?? this.pattern,
75+
);
76+
}
77+
78+
/// Creates a [TextInputFormatterModel] instance from a JSON object.
79+
factory TextInputFormatterModel.fromJson(Map json) =>
80+
_$TextInputFormatterModelFromJson(json);
81+
82+
@override
83+
Map toJson() => _$TextInputFormatterModelToJson(this);
84+
85+
@override
86+
List<Object?> get props => [name];
87+
}

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

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import 'package:codelessly_json_annotation/codelessly_json_annotation.dart';
2+
import 'package:equatable/equatable.dart';
3+
4+
import '../mixins.dart';
5+
6+
part 'text_input_validator_model.g.dart';
7+
8+
/// Text formatters that can be applied to the text field input to restrict the
9+
/// input to a specific format.
10+
@JsonSerializable()
11+
class TextInputValidatorModel with EquatableMixin, SerializableMixin {
12+
/// The regular expression to match the text.
13+
final String pattern;
14+
15+
/// The name of the formatter.
16+
final String name;
17+
18+
/// The error message to show when the input is invalid.
19+
final String errorMessage;
20+
21+
/// Allow only digits in the text field.
22+
static const TextInputValidatorModel none = TextInputValidatorModel(
23+
name: 'None',
24+
pattern: r'.*',
25+
errorMessage: '',
26+
);
27+
28+
/// Allow only digits in the text field.
29+
static const TextInputValidatorModel digitsOnly = TextInputValidatorModel(
30+
name: 'Digits Only',
31+
pattern: r'[0-9]',
32+
errorMessage: 'Only digits are allowed.',
33+
);
34+
35+
/// List of all available text field formatters.
36+
static const List<TextInputValidatorModel> values = [
37+
none,
38+
digitsOnly,
39+
];
40+
41+
/// Returns true if the formatter is [none].
42+
bool get isNone => this == none;
43+
44+
/// Creates a new [TextInputValidatorModel] instance with the given pattern.
45+
const TextInputValidatorModel({
46+
required this.name,
47+
required this.pattern,
48+
required this.errorMessage,
49+
});
50+
51+
/// Validates the input against the pattern. This can be used to use
52+
/// this class as a function.
53+
String? call(String? value) => validate(value);
54+
55+
/// Validates the input against the pattern.
56+
String? validate(String? input) {
57+
// check if the input perfectly matches the pattern.
58+
if (RegExp(pattern).firstMatch(input ?? '')
59+
case RegExpMatch(start: var start, end: var end)) {
60+
if (start == 0 && end == (input?.length ?? 0)) {
61+
// A perfect match. This is valid.
62+
return null;
63+
}
64+
}
65+
return errorMessage;
66+
}
67+
68+
/// copyWith
69+
TextInputValidatorModel copyWith({
70+
String? name,
71+
String? pattern,
72+
String? errorMessage,
73+
}) {
74+
return TextInputValidatorModel(
75+
name: name ?? this.name,
76+
pattern: pattern ?? this.pattern,
77+
errorMessage: errorMessage ?? this.errorMessage,
78+
);
79+
}
80+
81+
/// Creates a [TextInputValidatorModel] instance from a JSON object.
82+
factory TextInputValidatorModel.fromJson(Map json) =>
83+
_$TextInputValidatorModelFromJson(json);
84+
85+
@override
86+
Map toJson() => _$TextInputValidatorModelToJson(this);
87+
88+
@override
89+
List<Object?> get props => [name];
90+
}

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

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

lib/src/api/nodes/expansion_tile_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.

lib/src/api/nodes/floating_action_button_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.

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

Lines changed: 5 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/icon_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.

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

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

0 commit comments

Comments
 (0)