Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to keep field defaults updated to current values? #341

Open
all-iver opened this issue Nov 16, 2017 · 6 comments
Open

How to keep field defaults updated to current values? #341

all-iver opened this issue Nov 16, 2017 · 6 comments

Comments

@all-iver
Copy link

Say I have a form that's mapped to a model's fields. After submitting the form I want the dirty fields to be updated so that the saved values are no longer considered dirty. As far as I can tell, I need to update the field defaults to the saved values. With nested fields it gets pretty cumbersome because I can't just do something like form.update_defaults(saved_values). How can I easily map my model's updated values back onto the field defaults after a save?

Say with a schema like this:

fields = [
name: {},
email: {},
address: { fields: [{ line1: {}, line2: {}, city: {}, ... }]
]

In onSuccess() I can do form.values() to get all the dirty values, and after saving successfully I can do something like _assign(myModel, values) to update my model to the new values. But I can't see an easy way to get the new values into each field's defaults without some weird iteration and name mangling. Am I misunderstanding something about this workflow?

@foxhound87
Copy link
Owner

I think you need .set('default', { ... })

@all-iver
Copy link
Author

Aha, that does seem to work, thanks! I didn't understand what those functions did from the documentation. I just changed it to:

form.set('default', values)

@all-iver
Copy link
Author

It seems that set('defaults', values) does not trigger a field's input converter to run. If I do field.default = value then my input converter runs, but if I do form.set('defaults', values) the default value ends up as a pre-converted object instead, which means the field thinks it's dirty again (since field.value does not equal field.default). Am I still doing something wrong?

@foxhound87
Copy link
Owner

Note that you have to use default and not defaults.

I don't know if I don't understood your message, but I would do just:

form.set('default', form.values())

@all-iver
Copy link
Author

all-iver commented Nov 20, 2017

Oops, that was just a typo on my end. It is default and not defaults. The problem is that if the default is set using form.set(), it does not have the field's input function applied to it, but doing field.default = value does apply the input function. This might be related to #337?

@kapilpipaliya
Copy link

kapilpipaliya commented Jun 28, 2018

i have: const formHash = { fields: [ "id", "rank", "images", "images[].id", "images[].rank", "images[].imageUrl", "images[].image", "images[].isMain" ],

when i set values, if the values not have "images[].image" field in it, i can't display image field. it says: The selected field is not defined (image)

newStyleForm = new Form({ ...formHash, values })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants