mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2024-07-08 23:13:33 +00:00
NaiJi
4c99579f13
1. Refactor string validation classes 2. Rename string validation assets for length 3. Improve exception handling when server is not able to create requested server
52 lines
1.3 KiB
Dart
52 lines
1.3 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:cubit_form/cubit_form.dart';
|
|
import 'package:easy_localization/easy_localization.dart';
|
|
import 'package:selfprivacy/logic/api_maps/hetzner.dart';
|
|
import 'package:selfprivacy/logic/cubit/app_config/app_config_cubit.dart';
|
|
import 'package:selfprivacy/logic/cubit/forms/validations/validations.dart';
|
|
|
|
class HetznerFormCubit extends FormCubit {
|
|
HetznerFormCubit(this.initializingCubit) {
|
|
var regExp = RegExp(r"\s+|[-!$%^&*()@+|~=`{}\[\]:<>?,.\/]");
|
|
apiKey = FieldCubit(
|
|
initalValue: '',
|
|
validations: [
|
|
RequiredStringValidation('validations.required'.tr()),
|
|
ValidationModel<String>(
|
|
(s) => regExp.hasMatch(s), 'validations.key_format'.tr()),
|
|
LengthStringNotEqualValidation(64)
|
|
],
|
|
);
|
|
|
|
super.addFields([apiKey]);
|
|
}
|
|
|
|
@override
|
|
FutureOr<void> onSubmit() async {
|
|
initializingCubit.setHetznerKey(apiKey.state.value);
|
|
}
|
|
|
|
final AppConfigCubit initializingCubit;
|
|
|
|
late final FieldCubit<String> apiKey;
|
|
|
|
@override
|
|
FutureOr<bool> asyncValidation() async {
|
|
late bool isKeyValid;
|
|
HetznerApi apiClient = HetznerApi(isWithToken: false);
|
|
|
|
try {
|
|
isKeyValid = await apiClient.isValid(apiKey.state.value);
|
|
} catch (e) {
|
|
addError(e);
|
|
}
|
|
|
|
if (!isKeyValid) {
|
|
apiKey.setError('bad key');
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|