add validation

master
Kherel 2 years ago
parent 65c6a0b870
commit 3a5353dbf4
  1. 10
      assets/translations/en.json
  2. 7
      assets/translations/ru.json
  3. 11
      lib/logic/cubit/forms/user/user_form_cubit.dart
  4. 20
      lib/ui/pages/users/new_user.dart
  5. 1
      lib/ui/pages/users/users.dart

@ -204,7 +204,6 @@
"6": "Delete the server and volume?",
"7": "Yes",
"8": "Remove task"
},
"timer": {
"sec": "{} sec"
@ -216,10 +215,11 @@
"empty": "No jobs"
},
"validations": {
"required": "required",
"invalid_format": "invalid format",
"required": "Required",
"invalid_format": "Invalid format",
"root_name": "User name cannot be 'root'",
"key_format": "invalid key format",
"length": "length is [] shoud be {}"
"key_format": "Invalid key format",
"length": "Length is [] shoud be {}",
"user_alredy_exist": "Already exists"
}
}

@ -216,9 +216,10 @@
},
"validations": {
"required": "обязательное поле",
"invalid_format": "неверный формат",
"invalid_format": "Неверный формат",
"root_name": "Имя пользователя не может быть'root'",
"key_format": "неверный формат",
"length": "Длина строки [] должна быть {}"
"key_format": "Неверный формат",
"length": "Длина строки [] должна быть {}",
"user_alredy_exist": "Имя уже используется"
}
}

@ -9,7 +9,8 @@ import 'package:easy_localization/easy_localization.dart';
class UserFormCubit extends FormCubit {
UserFormCubit({
required this.usersCubit,
required this.jobsCubit,
required List<User> users,
User? user,
}) {
var isEdit = user != null;
@ -20,6 +21,10 @@ class UserFormCubit extends FormCubit {
login = FieldCubit(
initalValue: isEdit ? user!.login : '',
validations: [
ValidationModel(
(login) => users.any((user) => user.login == login),
'validations.user_alredy_exist'.tr(),
),
RequiredStringValidation('validations.required'.tr()),
ValidationModel<String>(
(s) => userRegExp.hasMatch(s), 'validations.invalid_format'.tr()),
@ -44,7 +49,7 @@ class UserFormCubit extends FormCubit {
login: login.state.value,
password: password.state.value,
);
usersCubit.addJob(CreateUserJob(user: user));
jobsCubit.addJob(CreateUserJob(user: user));
}
late FieldCubit<String> login;
@ -54,5 +59,5 @@ class UserFormCubit extends FormCubit {
password.externalSetValue(genPass());
}
late JobsCubit usersCubit;
final JobsCubit jobsCubit;
}

@ -9,8 +9,24 @@ class _NewUser extends StatelessWidget {
return BrandBottomSheet(
child: BlocProvider(
create: (context) =>
UserFormCubit(usersCubit: context.read<JobsCubit>()),
create: (context) {
var jobCubit = context.read<JobsCubit>();
var jobState = jobCubit.state;
var users = <User>[];
users.addAll(context.read<UsersCubit>().state.users);
if (jobState is JobsStateWithJobs) {
var jobs = jobState.jobList;
jobs.forEach((job) {
if (job is CreateUserJob) {
users.add(job.user);
}
});
}
return UserFormCubit(
jobsCubit: jobCubit,
users: users,
);
},
child: Builder(builder: (context) {
var formCubitState = context.watch<UserFormCubit>().state;

@ -6,6 +6,7 @@ import 'package:selfprivacy/logic/cubit/app_config/app_config_cubit.dart';
import 'package:selfprivacy/logic/cubit/forms/user/user_form_cubit.dart';
import 'package:selfprivacy/logic/cubit/jobs/jobs_cubit.dart';
import 'package:selfprivacy/logic/cubit/users/users_cubit.dart';
import 'package:selfprivacy/logic/models/jobs/job.dart';
import 'package:selfprivacy/logic/models/user.dart';
import 'package:selfprivacy/ui/components/brand_bottom_sheet/brand_bottom_sheet.dart';
import 'package:selfprivacy/ui/components/brand_button/brand_button.dart';

Loading…
Cancel
Save