Question T2245
Visible to All Users

Property default values aren't serialized

created 6 years ago

I'm trying to add a custom name property to all my user surveys, but an issue arises where the default value doesn't get serialized when saving, even though the default value correctly appears in the survey property grid. This happens while using any of the following methods:

JavaScript
SurveyKo.Serializer.addProperty("survey", { name: "name:text", default: "Unnamed Survey" });
JavaScript
SurveyKo.Serializer.findProperty("survey", "name").defaultValue = "Unnamed survey";
JavaScript
SurveyKo.Serializer.findProperty("survey", "name").defaultValueValue = "Unnamed survey";

Any ideas on how to get around the issue?

Answers approved by surveyjs Support

created 6 years ago

Hello Sami,
Serializer use the default attribute for existing properties in the following way - it doesn't serialize the property value if it equals to default value to keep JSON smaller.
You may easily solve you problem by adding the following code, after loading survey into the creator or on creating a survey:

JavaScript
if(!creator.survey.name) { creatur.survey.name = "Unnamed survey"; }

That is all. If you are creating a new survey or an old survey came from your database with empty name, then set the default name.

Thank you,
Andrew
SurveyJS Team