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

Added defaultValue property to Multipletextitem - DC #72

Closed

Conversation

ChopinDavid
Copy link
Contributor

This is being done in an effort to resolve #70

@goxiaoy
Copy link
Owner

goxiaoy commented May 17, 2023

Try this json in https://surveyjs.io/create-free-survey

{
 "logoPosition": "right",
 "pages": [
  {
   "name": "page1",
   "elements": [
    {
     "type": "multipletext",
     "name": "question1",
     "items": [
      {
       "name": "text1",
       "defaultValue":"abc",
      },
      {
       "name": "text2"
      }
     ]
    }
   ]
  }
 ]
}

It shows error
image

I believe that you would like to use the defaultValue in Multipletext instead of Multipletextitem

@goxiaoy goxiaoy closed this May 17, 2023
@ChopinDavid
Copy link
Contributor Author

ChopinDavid commented May 17, 2023

@goxiaoy You're correct, that is my fault. I was thinking I would need Multipletextitem.defaultValue to exist so that I could map it to Text.defaultValue in multiple_text.dart lines 47-56 much like the other elements were:

s.Text toText(s.Multipletextitem multipleTextItem) {
  final b = s.Text().toBuilder()
    ..type = "text"
    ..name = multipleTextItem.name
    ..isRequired = multipleTextItem.isRequired
    ..validators = ListBuilder(multipleTextItem.validators?.toList() ?? [])
    ..inputType = s.TextInputType.valueOf(multipleTextItem.inputType.toString())
    ..title = multipleTextItem.title
    ..maxLength = multipleTextItem.maxLength
    ..size = multipleTextItem.size
    ..requiredErrorText = multipleTextItem.requiredErrorText
    ..placeholder = multipleTextItem.placeholder;
    //.defaultValue = multipleTextItem.defaultValue;

  return b.build();
}

Of course, we see in #74 that we end up passing JsonObject? defaultValue to toText, which negates the need for multipleTextItem.defaultValue to exist:

s.Text toText(s.Multipletextitem multipleTextItem, JsonObject? defaultValue) {
  final Object? defaultValueForItem = defaultValue is MapJsonObject
      ? defaultValue.value[multipleTextItem.name]
      : defaultValue;
  final b = s.Text().toBuilder()
    ..type = "text"
    ..name = multipleTextItem.name
    ..isRequired = multipleTextItem.isRequired
    ..validators = ListBuilder(multipleTextItem.validators?.toList() ?? [])
    ..inputType = s.TextInputType.valueOf(multipleTextItem.inputType.toString())
    ..title = multipleTextItem.title
    ..maxLength = multipleTextItem.maxLength
    ..size = multipleTextItem.size
    ..requiredErrorText = multipleTextItem.requiredErrorText
    ..placeholder = multipleTextItem.placeholder
    ..defaultValue =
        defaultValueForItem == null ? null : JsonObject(defaultValueForItem);

  return b.build();
}

@ChopinDavid ChopinDavid deleted the AddingDefaultValueToMultipletextitem branch May 17, 2023 15:08
@goxiaoy
Copy link
Owner

goxiaoy commented May 17, 2023

@ChopinDavid This is handled now

AbstractControl multipleTextControlBuilder(
BuildContext context, s.Elementbase element,
{validators = const []}) {
final e = element as s.Multipletext;
final texts = (e.items?.toList() ?? []).map(toText).toList();
final res = elementsToFormGroup(context, texts,
validators: validators, value: e.defaultValue?.value);
return res;
}

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

Successfully merging this pull request may close these issues.

Feature: add defaultValue support for all elements
2 participants