goxiaoy / flutter_survey_js

Flutter client library for parsing and display surveyjs.io survey
https://goxiaoy.github.io/flutter_survey_js/
MIT License
16 stars 17 forks source link

Make `removeEmptyField` configurable in `SurveyWidget.submit()` #128

Closed ChopinDavid closed 3 months ago

ChopinDavid commented 3 months ago

Describe the bug When no non-required elements have no answer, they are removed from the JSON passed in SurveyWidget's onSubmit method invocation.

To Reproduce Submit the following survey without providing any answer to "question1".

SurveyJs json

{
 "logoPosition": "right",
 "pages": [
  {
   "name": "page1",
   "elements": [
    {
     "type": "checkbox",
     "name": "question1",
     "choices": [
      "Item 1",
      "Item 2",
      "Item 3"
     ]
    }
   ]
  }
 ]
}

SurveyJs answer

{}

Expected behavior Currently, we always receive a

{}

response.

Ideally, there should be a way to pass a value, say removingEmptyFields, to SurveyWidget. If this value were false, we would not remove empty fields from the JSON passed to the SurveyWidget.onSubmit invocation. Then, we would expect the response to look like:

{
  "question1": []
}

This could be accomplished via:

final bool removingEmptyFields;

const SurveyWidget({
  Key? key,
  required this.survey,
  this.answer,
  this.onSubmit,
  this.onErrors,
  this.onChange,
  this.controller,
  this.builder,
  this.removingEmptyFields = true,
}) : super(key: key);

and

widget.onSubmit?.call(widget.removingEmptyFields ? removeEmptyField(formGroup.value) : formGroup.value);