Giter VIP home page Giter VIP logo

Comments (10)

vasilich6107 avatar vasilich6107 commented on August 16, 2024

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.

BenjiFarquhar avatar BenjiFarquhar commented on August 16, 2024

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.

aabegg avatar aabegg commented on August 16, 2024

@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.

BenjiFarquhar avatar BenjiFarquhar commented on August 16, 2024

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.

vasilich6107 avatar vasilich6107 commented on August 16, 2024

With latest beta it should update.
Added demo in example project

from reactive_forms_generator.

BenjiFarquhar avatar BenjiFarquhar commented on August 16, 2024

@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.

aabegg avatar aabegg commented on August 16, 2024

@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.

vasilich6107 avatar vasilich6107 commented on August 16, 2024

@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.

BenjiFarquhar avatar BenjiFarquhar commented on August 16, 2024

@aabegg You're welcome.

@vasilich6107 I see, thank you.

from reactive_forms_generator.

vasilich6107 avatar vasilich6107 commented on August 16, 2024

If there is no more questions - I'm closing the issue.
Fill free to reopen

from reactive_forms_generator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.