Skip to content

Commit

Permalink
Merge pull request #37 from yeikel16/fix-pure-value
Browse files Browse the repository at this point in the history
fix(lyform):  validation when the form is pure value state
  • Loading branch information
leynier authored Jan 4, 2024
2 parents 6c47f03 + f5123e1 commit 0069658
Show file tree
Hide file tree
Showing 25 changed files with 443 additions and 62 deletions.
2 changes: 1 addition & 1 deletion packages/flutter_lyform/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:example/profile_form/profile_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

Future<void> main() async {
void main() {
Bloc.observer = AppBlocObserver();
runApp(const App());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:example/profile_form/profile_form.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_lyform/flutter_lyform.dart';

Expand Down Expand Up @@ -87,8 +88,12 @@ class ProfileView extends StatelessWidget {
builder: (context, state) {
final bloc = context.read<ProfileForm>().age;
return TextField(
onChanged: (value) => bloc.dirty(int.parse(value)),
onChanged: (value) =>
bloc.dirty(value.isEmpty ? 0 : int.parse(value)),
keyboardType: TextInputType.number,
inputFormatters: [
FilteringTextInputFormatter.digitsOnly,
],
decoration: InputDecoration(
hintText: 'Age',
errorText: state.error,
Expand All @@ -99,6 +104,11 @@ class ProfileView extends StatelessWidget {
const SizedBox(height: 16),
LyFormBuilder<ProfileForm>(
bloc: context.read<ProfileForm>(),
onPure: () => const MaterialButton(
color: Colors.blueGrey,
onPressed: null,
child: Text('Save Profile'),
),
onLoading: () => const Center(
child: CircularProgressIndicator(),
),
Expand Down
14 changes: 9 additions & 5 deletions packages/flutter_lyform/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ publish_to: "none"
version: 1.0.0+1

environment:
sdk: ">=2.17.1 <3.0.0"
sdk: ">=3.0.0 <4.0.0"
flutter: ">=3.10.0"

dependencies:
flutter:
sdk: flutter
flutter_bloc: ^8.1.1
flutter_lyform: ^0.13.0
lyform_validators: ^0.13.0
flutter_bloc: ^8.1.3
flutter_lyform:
path: ../../flutter_lyform
lyform_validators:
path: ../../lyform_validators

dev_dependencies:
lint: ^1.10.0
lint: ^2.2.0

flutter:
uses-material-design: true
1 change: 0 additions & 1 deletion packages/flutter_lyform/lib/flutter_lyform.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
library flutter_lyform;

export 'package:lyform/lyform.dart';

Expand Down
14 changes: 7 additions & 7 deletions packages/flutter_lyform/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ homepage: https://github.com/lynotofficial/lynot-dart-flutter
repository: https://github.com/lynotofficial/lynot-dart-flutter/tree/master/packages/flutter_lyform

environment:
sdk: ">=2.17.0 <3.0.0"
flutter: ">=1.17.0"
sdk: ">=3.0.0 <4.0.0"
flutter: ">=3.10.0"

dependencies:
bloc: ^8.1.0
bloc: ^8.1.2
flutter:
sdk: flutter
flutter_bloc: ^8.1.1
lyform: ^0.13.0
flutter_bloc: ^8.1.3
lyform: ^0.13.0+1
dev_dependencies:
flutter_test:
sdk: flutter
lint: ^1.10.0
mocktail: ^0.3.0
lint: ^2.2.0
mocktail: ^1.0.1
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ publish_to: 'none'
version: 1.0.0+1

environment:
sdk: '>=2.18.0 <3.0.0'
sdk: '>=3.0.0 <4.0.0'

dependencies:
flutter:
sdk: flutter

cupertino_icons: ^1.0.2
cupertino_icons: ^1.0.6
flutter_riverform: ^0.1.0
flutter_riverpod: ^2.1.1
flutter_riverpod: ^2.4.8

dev_dependencies:
flutter_test:
sdk: flutter

flutter_lints: ^2.0.0
flutter_lints: ^2.0.3

flutter:
uses-material-design: true
1 change: 0 additions & 1 deletion packages/flutter_riverform/lib/flutter_riverform.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
library flutter_riverform;

export 'package:riverform/riverform.dart';

Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_riverform/lib/src/riverform_scope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class RiverformScope extends ConsumerWidget {
for (final input in form.inputs)
input.initialValueProvider.overrideWithProvider(
(argument) => StateProvider((ref) => initialValues[input.id]),
)
),
],
child: RiverformProvider(
form: form,
Expand Down
4 changes: 2 additions & 2 deletions packages/flutter_riverform/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ environment:
dependencies:
flutter:
sdk: flutter
flutter_riverpod: ^2.1.1
flutter_riverpod: ^2.4.8
riverform: ^0.1.0
dev_dependencies:
flutter_test:
sdk: flutter
lint: ^1.10.0
lint: ^2.2.0

flutter:
2 changes: 0 additions & 2 deletions packages/lyform/lib/lyform.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
library lyform;

export 'src/ly_form/ly_form.dart';
export 'src/ly_input/ly_input.dart';
5 changes: 3 additions & 2 deletions packages/lyform/lib/src/ly_form/ly_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ abstract class LyForm<D, E> extends Bloc<LyFormEvent, LyFormState<D, E>>
return _inputs.every((input) => input.isValid);
}

bool isInvalid() => !isValid();

/// Are every input Pure?
bool isPure() {
return _inputs.every((input) => input.isPure);
Expand Down Expand Up @@ -322,8 +324,7 @@ abstract class LyForm<D, E> extends Bloc<LyFormEvent, LyFormState<D, E>>
LyInput<dynamic> Function(
LyInput<dynamic> value,
LyInput<dynamic> element,
)
combine,
) combine,
) =>
_inputs.reduce(combine);

Expand Down
11 changes: 9 additions & 2 deletions packages/lyform/lib/src/ly_input/ly_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class LyInput<T> extends Bloc<LyInputEvent<T>, LyInputState<T>> {
debugName: debugName,
),
);
if (validationType == LyValidationType.always) {
if (validationType != LyValidationType.none) {
validate();
}
onPostDirty?.call(this, event.value);
Expand Down Expand Up @@ -122,7 +122,14 @@ class LyInput<T> extends Bloc<LyInputEvent<T>, LyInputState<T>> {
final LyValidationType validationType;
final String? debugName;

bool get isValid => isPure || (error?.isEmpty ?? true);
bool get isValid {
if (validationType.isAlways || (!isPure && validationType.isExplicit)) {
return validator?.call(value) == null;
}

return true;
}

bool get isInvalid => !isValid;
bool get isPure => pureValue == value;

Expand Down
11 changes: 10 additions & 1 deletion packages/lyform/lib/src/ly_input/ly_validation_type.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
part of 'ly_input.dart';

enum LyValidationType {
/// Not validate the field
none,

/// Only validate the field when is drity
explicit,
always,

/// Always validate the field
always;

bool get isNone => this == none;
bool get isExplicit => this == explicit;
bool get isAlways => this == always;
}
16 changes: 8 additions & 8 deletions packages/lyform/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ homepage: https://github.com/lynotofficial/lynot-dart-flutter
repository: https://github.com/lynotofficial/lynot-dart-flutter/tree/master/packages/lyform

environment:
sdk: ">=2.17.0 <3.0.0"
sdk: ">=3.0.0 <4.0.0"

dependencies:
bloc: ^8.1.0
bloc_concurrency: ^0.2.0
collection: ^1.16.0
bloc: ^8.1.2
bloc_concurrency: ^0.2.2
collection: ^1.18.0
equatable: ^2.0.5

dev_dependencies:
bloc_test: ^9.1.0
lint: ^1.10.0
mocktail: ">=0.3.0 <2.0.0"
test: ^1.21.4
bloc_test: ^9.1.5
lint: ^2.2.0
mocktail: ^1.0.1
test: ^1.24.9
29 changes: 29 additions & 0 deletions packages/lyform/test/mocks/mock_forms.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,35 @@ class TestForm extends LyForm<String, String> {
}
}

class TestPureForm extends LyForm<String, String> {
TestPureForm() {
addInputs([
name,
lastname,
]);
}

final name = LyInput<String>(
pureValue: 'unknow',
validationType: LyValidationType.always,
validator: const LyStringRequired('Invalid name.'),
debugName: 'name',
);

final lastname = LyInput<String>(
pureValue: 'unknow',
validationType: LyValidationType.explicit,
validator: const LyStringRequired('Invalid lastname.'),
debugName: 'lastname',
);

@override
Stream<LyFormState<String, String>> onSubmit() async* {
await Future<void>.delayed(const Duration(seconds: 1));
yield success('Name save success!');
}
}

class MyBlocObserver extends BlocObserver {
@override
void onTransition(Bloc bloc, Transition transition) {
Expand Down
Loading

0 comments on commit 0069658

Please sign in to comment.