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

Get Whole form in JSON with changed value #73

Open
sushil7271 opened this issue Jun 26, 2020 · 3 comments
Open

Get Whole form in JSON with changed value #73

sushil7271 opened this issue Jun 26, 2020 · 3 comments

Comments

@sushil7271
Copy link

sushil7271 commented Jun 26, 2020

Hi,
Is there any way I can get the whole form with values changed by the user?
In my case, I have to maintain the version of the forms with changed data.
Thank you

@sushil7271 sushil7271 changed the title Get changed value Get Whole form in JSON with changed value Jun 27, 2020
@OndrejKunc
Copy link
Owner

Hi,

The preferred way to collect data from the form is via the:

List<FormItemValue> data = formManager.getFormData()

As described in readme:

It contains a list of all the properties which were marked as a mutable in a component parser definition. In default components those are the properties that are expected to be changed by a user. Each item contains id of the source element, property name, and property value. To submit the form you usually want to serialize this list and send it back to your server.

This was useful in our projects because we didn't want to send the whole form back (because it is a lot of data that server already knows about) so we were sending only those triplets.

Unfortunately, there is no functionality of serializing the model tree back to the JSON - this library cares only about deserialization. However, I think this is achievable without any modification of the library. You just need your root FormElement and use the
Map<String, Property> properties property to recursively traverse the whole model tree and create a JSON document with the updated values.

@sushil7271
Copy link
Author

Thank you for your response.
Could you please help me to achieve this with one example.

@pritamgudekar
Copy link

pritamgudekar commented Nov 22, 2024

As a beginner to flutter and going through this library, instead of serializing the model to the JSON (may consist of unknown depth of the tree) we can extract the FormElement and set it's value using Map<String, Property> properties. For a list of values you no need to recursively traverse the whole model tree but traverse till the # of values to be set. You can refer the following pseudo method.:

In this library there is getFormProperties() but not for setting the properties.

// Function to set form properties
void setFormProperties(Map<String, FormElement> formElementMap, List<FormPropertyValue> propertyList) {
  for (var propertyValue in propertyList) {
    var formElement = formElementMap[propertyValue.id];
    if (formElement != null) {
      var property = formElement.getProperty(propertyValue.property);
      if (property is MutableProperty) {
        var value = parsePropertyValue(property, propertyValue.value);
        // Here the value may be having different types, you can parse it further by checking the type of the value.
        property.setValue(value);
      }
    }
  }
}

I hope this will help further :)

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

No branches or pull requests

3 participants