surveyjs / survey-creator

Scalable open-source survey software to generate dynamic JSON-driven forms within your JavaScript application. The form builder features a drag-and-drop UI, CSS Theme Editor, and GUI for conditional logic and form branching.
https://surveyjs.io/open-source
Other
915 stars 373 forks source link

Specialized Components - The inheritBaseProps cannot be modified in a creator #5339

Closed JaneSjs closed 8 months ago

JaneSjs commented 8 months ago

T17332 - isRequired in custom question types https://surveyjs.answerdesk.io/internal/ticket/details/T17332


The isRequired property is defined within an array of properties inherited from a base Text question. The isRequired option is defined within the custom component configuration.

Survey.ComponentCollection.Instance.add({
    name: "country",
    title: "Country",
    inheritBaseProps: ["isRequired"],
    questionJSON: {
        type: "text",
        placeholder: "Select a country...",
        isRequired: true,
    },
});

However, if you uncheck this option in designer, the option remains enabled in Preview: View Demo.

![Uploading image.png…]()

andrewtelnov commented 8 months ago

It is impossible to achieve the goal by using this path. isRequired is "false" by default and we do not store in JSON the default properties values, otherwise our JSONs would be huge. Because, there is no "false" property set into JSON, there is noway to override your "default" true value that you set in the internal question. The task has to be implemented in the different way. Basically, we want to have another default value for isRequired property for "country" question type. These two lines should be removed: inheritBaseProps: ["isRequired"], and isRequired: true and set the default value for isRequired for "country" question type:

Survey.Serializer.getProperty("country", "isRequired").defaultValue = true;

findProperty function can't be used because it will return the base question "isRequired" property. By modifying it's attributes, the behavior of all questions will be modified. getProperty function should be used instead. It will create the "copy" of the base "isRequired" property. All it's attributes will be copied and the new property defintion will be related to "country" type and it's descendants.

Here is the final code:

Survey.ComponentCollection.Instance.add({
    name: "country",
    title: "Country",
    questionJSON: {
        type: "text",
        placeholder: "Select a country...",
    },
});
Survey.Serializer.getProperty("country", "isRequired").defaultValue = true;

Here is the working example.

Thank you, Andrew