question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bind form fields values to actual objects

See original GitHub issue

If I understand correctly, the current implementation of mobx-react-form is one-way binding - we can pass some initial values to the fields, but after that, the state is maintained internally within the form object and loses completely it’s connection to initial object. This is less than ideal. In true object-oriented manner, that mobx is encouraging to use, one would bind directly to an @observable value. This means, I could do something like this:

@observable formData = {
        firstName: 'hai'
};

this.form = new MobxReactForm(
        {
            fields: {
                firstName: {
                    label: 'First Name',
                    placeholder: 'Type Firstname',
                    rules: 'required|string|between:5,25',
                    value: this.formData.firstName
                }
        },
        {
            plugins: { dvr: validatorjs }
        }
);

and when interacting directly with input (typing in new value), that value would automatically land back in this.formData.firstName, effectively giving us the two-way databinding.

The current solution of asking for form.values() upon submitting/validating and then copying them back to the appropriate objects seems needlessly cumbersome. Am I missing something here? Is there an easy way to achieve this? And by easy I mean other than multiple declarations of form.$('field').observe(({ form, field, change }) => { ... }); ?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:22 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
rdiazvcommented, Apr 21, 2017

I have a MyForm component that is bound to a model (mobx-rest) and a form (mobx-react-form):

  • When MyForm is going to mount the model assigns all its values to the form.
  • A reaction watches the form values so that whenever there is a change it sets the new form values on the model.
  • The reaction gets disposed when MyForm unmounts.

The point is that you can easily set a reaction to watch form.values() and do whatever your app needs, this way you don’t have to define a per-field observer.

2reactions
sebastian-zarzycki-apzumicommented, Apr 24, 2017

Ah, I see. Yeah, it’s the same approach as I’ve described few posts above. Yes, fortunately, there are ways to get events upon form changing and then react to these events and write back the form values to the model.

I just wanted to go a step further and make the most common case embedded directly into the framework, so that it can update the model values itself.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Object Binding with Forms
Object binding lets you associate one or more business objects with a Salesforce B2C Commerce form instance and associate form elements with specific ......
Read more >
Binding an array of objects to their specific form fields for ...
I'm able to store the form data as objects in an array, but can't figure out how to load it back into the...
Read more >
Binding data to form fields - Adobe Support
If a control has both an id and a name attribute, use either value. You can bind to any ColdFusion form control, including...
Read more >
Binding Data to Forms | Data Binding | Vaadin Docs
How to Bind Form Data ; // Start by defining the Field instance to use ·.forField(titleField) // Finalize by doing the actual binding...
Read more >
Binding Class (System.Windows.Forms) - Microsoft Learn
Represents the simple binding between the property value of an object and the property value of a control.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found