Comments (10)
Hi
I do not understand your question.
form manages the state itself, UI represents the fields and toggles.
why do you need riverpod?
from reactive_forms_generator.
Just add a FormModel
variable to your bussiness logic class, then do whatever you want with it from that class. Or you can just not retain the formModel, and pass it into your business logic functions if you prefer. The FormBuilder generated class has an initState
property so you can set the business logic formModel there.
form definition:
import 'package:reactive_forms_annotations/reactive_forms_annotations.dart';
part 'email_form.gform.dart';
@ReactiveFormAnnotation()
class Email {
final String email;
Email({
@FormControlAnnotation() this.email = '',
});
}
widget with FormBuilder
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:reactive_forms_annotations/reactive_forms_annotations.dart';
import 'email_form.dart';
import 'email_logic.dart';
class EmailWidget extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final emailBusinessLogic = ref.watch(emailLogicProvider);
return EmailFormBuilder(
model: emailBusinessLogic.model,
initState: emailBusinessLogic.initState,
builder: (context, formModel, child) {
return Column(children: [
ReactiveTextField(formControlName: EmailForm.emailControlName),
ElevatedButton(
child: Text('Save'),
onPressed: () => emailBusinessLogic.save(),
)
]);
});
}
}
Business logic
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:reactive_forms_annotations/reactive_forms_annotations.dart';
import 'email_form.dart';
final emailLogicProvider = Provider((ref) {
return EmailLogic(ref.read(myApiRepositoryProvider));
});
class EmailLogic {
FormModel? formModel;
final MyApiRepository myApiRepository;
EmailLogic(this.myApiRepository);
Email get model => Email();
void initState(BuildContext context, FormModel formModel) {
this.formModel = formModel;
// perform some form logic here if you like
formModel?.emailControl!
.setAsyncValidators([emailAddressUnique], autoValidate: false);
formModel?.emailControl!.markAsDirty();
}
Future save() {
return myApiRepository.saveEmailAddress(
emailAddress: formModel?.model.emailAddress);
}
}
from reactive_forms_generator.
@BenjiFarquhar Thank you for your reply. This is exactly what I meant.
I have the following additional question:
In my case, I request the data via REST when I edit. The JSON data is then converted via JsonSerializable to the e.g. Email Class (As in your example).
Now is there a way to patch the form directly with the EmailClass or does this always have to be done via initState or thus Map?
Thanks for your feedback
from reactive_forms_generator.
Do you mean to update the form from your business logic? Populate your model initially passed into EmailFormBuilder.model
, and it will show in the form's values. Subsequently, to update the form from the business logic, use formModel.updateValue(Email(email: '[email protected]'));
or the other updating methods on formModel
and formModel.form
.
@vasilich6107 Since you recently added didUpdateWidget
, should updating the model
value given to EmailFormBuilder.model
in my example, such as:
model = Email(email: '[email protected]');
trigger didUpdateWidget
to run? Which would mean the new values in the model would show in the form? It is just not doing that for me. Maybe it is not meant to.
from reactive_forms_generator.
With latest beta it should update.
Added demo in example
project
from reactive_forms_generator.
@vasilich6107 Thanks! Looks like reactive_touch_spin
needs to bump intl
to ^0.18.0
to be compatible with latest beta.
from reactive_forms_generator.
@BenjiFarquhar Sorry for the late reply. Yes that is exactly what I meant. I will test it today. Thanks for your efforts.
from reactive_forms_generator.
@BenjiFarquhar the same day after package maintainer will update his deps)
for now you;ll have to use overrides
https://github.com/danvick/flutter_touch_spin/blob/master/pubspec.yaml
from reactive_forms_generator.
@aabegg You're welcome.
@vasilich6107 I see, thank you.
from reactive_forms_generator.
If there is no more questions - I'm closing the issue.
Fill free to reopen
from reactive_forms_generator.
Related Issues (20)
- didUpdateWidget sets the form to pristine and untouched HOT 5
- How to properly dispose Form, dynamic fields/formgroup all at once? HOT 1
- How to use with freezed unions? HOT 3
- How to create a group that can be also a form in some situations. HOT 2
- There is a form.model getter? HOT 1
- [BUG] {Model}Form is generated for each file that use it as a import. HOT 6
- Unexpected warning message for nested form models HOT 6
- test HOT 4
- form.model is wrong when use a nested array of groups HOT 7
- [Feature Request] add submit method to FormModel HOT 3
- reactive_forms_annotations not updated to 1.2.0 HOT 4
- Could not format because the source could not be parsed HOT 2
- MinValidator() don't work HOT 2
- Data are not binding HOT 24
- Example of a ReactiveFormsGenerator on ViewModel HOT 4
- The casing of key names of Form value is different from what Freezed package generated HOT 7
- The static formControlNames could be const HOT 2
- Validators set programmatically get cleared on didUpdateWidget HOT 1
- Programmatically Add controls HOT 2
- @FormArrayAnnotation() with inheritance HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reactive_forms_generator.