selfprivacy.org.app/lib/logic/cubit/forms/validations/validations.dart

29 lines
1 KiB
Dart
Raw Normal View History

2021-01-06 17:35:57 +00:00
import 'package:cubit_form/cubit_form.dart';
import 'package:easy_localization/easy_localization.dart';
2021-01-06 17:35:57 +00:00
abstract class LengthStringValidation extends ValidationModel<String> {
LengthStringValidation(bool Function(String) predicate, String errorMessage)
: super(predicate, errorMessage);
2021-01-06 17:35:57 +00:00
@override
String? check(String value) {
var length = value.length;
var errorMessage = this.errorMassage.replaceAll("[]", length.toString());
return test(value) ? errorMessage : null;
2021-01-06 17:35:57 +00:00
}
}
class LengthStringNotEqualValidation extends LengthStringValidation {
2022-05-04 19:32:24 +00:00
/// String must be equal to [length]
LengthStringNotEqualValidation(int length)
: super((n) => n.length != length,
'validations.length_not_equal'.tr(args: [length.toString()]));
}
class LengthStringLongerValidation extends LengthStringValidation {
2022-05-04 19:32:24 +00:00
/// String must be shorter than or equal to [length]
LengthStringLongerValidation(int length)
: super((n) => n.length > length,
'validations.length_longer'.tr(args: [length.toString()]));
}