feat(form): add list option validator
This commit is contained in:
@@ -23,16 +23,16 @@ import 'package:wyatt_form_bloc/src/form/form.dart';
|
||||
|
||||
part 'form_data_state.dart';
|
||||
|
||||
class FormDataCubit extends Cubit<FormDataState> {
|
||||
final Future<bool> Function(FormDataState state)? _onSubmit;
|
||||
abstract class FormDataCubit extends Cubit<FormDataState> {
|
||||
FormDataCubit({required FormData entries})
|
||||
: super(FormDataState(data: entries));
|
||||
|
||||
FormDataCubit({
|
||||
required FormData entries,
|
||||
Future<bool> Function(FormDataState state)? onSubmit,
|
||||
}) : _onSubmit = onSubmit,
|
||||
super(FormDataState(data: entries));
|
||||
|
||||
void dataChanged<T>(String field, FormInput dirtyValue) {
|
||||
/// Change value of a field.
|
||||
///
|
||||
/// Inputs:
|
||||
/// - `field`: The key of the field to change.
|
||||
/// - `dirtyValue`: The new value of the field.
|
||||
void dataChanged(String field, FormInput dirtyValue) {
|
||||
final _form = state.data.clone();
|
||||
|
||||
if (_form.contains(field)) {
|
||||
@@ -49,6 +49,11 @@ class FormDataCubit extends Cubit<FormDataState> {
|
||||
);
|
||||
}
|
||||
|
||||
/// Update entries list.
|
||||
///
|
||||
/// Inputs:
|
||||
/// - `data`: The new entries list.
|
||||
/// - `operation`: The operation to perform on the entries set.
|
||||
void updateFormData(
|
||||
FormData data, {
|
||||
SetOperation operation = SetOperation.replace,
|
||||
@@ -78,13 +83,6 @@ class FormDataCubit extends Cubit<FormDataState> {
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> submitForm() async {
|
||||
unawaited(
|
||||
_onSubmit?.call(state).then((bool reemit) {
|
||||
if (reemit) {
|
||||
emit(state);
|
||||
}
|
||||
}),
|
||||
);
|
||||
}
|
||||
/// Submit the form.
|
||||
Future<void> submitForm();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import 'package:wyatt_form_bloc/src/enums/enums.dart';
|
||||
import 'package:wyatt_form_bloc/src/form/form.dart';
|
||||
|
||||
/// {@template list_option}
|
||||
/// Form input for a list input
|
||||
/// {@endtemplate}
|
||||
class ListOption<T> extends FormInput<List<T>, ValidationError> {
|
||||
/// {@macro list_option}
|
||||
const ListOption.pure({List<T>? defaultValue})
|
||||
: super.pure(defaultValue ?? const []);
|
||||
|
||||
/// {@macro list_option}
|
||||
const ListOption.dirty({List<T>? value}) : super.dirty(value ?? const []);
|
||||
|
||||
ListOption select(T? v) {
|
||||
if (v == null) {
|
||||
return this;
|
||||
}
|
||||
if (value.contains(v)) {
|
||||
final List<T> newValue = List.from(value)..remove(v);
|
||||
return ListOption<T>.dirty(value: newValue);
|
||||
} else {
|
||||
final List<T> newValue = List.from(value)..add(v);
|
||||
return ListOption<T>.dirty(value: newValue);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
ValidationError? validator(List<T>? value) {
|
||||
return value?.isNotEmpty == true ? null : ValidationError.invalid;
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ export 'boolean.dart';
|
||||
export 'confirmed_password.dart';
|
||||
export 'email.dart';
|
||||
export 'iban.dart';
|
||||
export 'list_option.dart';
|
||||
export 'name.dart';
|
||||
export 'password.dart';
|
||||
export 'phone.dart';
|
||||
|
||||
Reference in New Issue
Block a user