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

default_value has no effect for DropdownQuestion #391

Open
rilshok opened this issue Jun 14, 2023 · 2 comments
Open

default_value has no effect for DropdownQuestion #391

rilshok opened this issue Jun 14, 2023 · 2 comments

Comments

@rilshok
Copy link

rilshok commented Jun 14, 2023

Hello! I have encountered an issue with using the default_value parameter in the questions package. When using this parameter for DropdownQuestion type questions, it does not apply and the default selected value is not displayed.

Here's a minimal code example that demonstrates this issue:

from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from questions import DropdownQuestion, Form, FormPage
import uvicorn

class Page(Form):
    person = DropdownQuestion(
        title="Full name",
        choices=["George Corbyn", "Stanley Castillo", "Ida Stanley"],
        default_value="Stanley Castillo",
    )

class Profile(Form):
    page = FormPage(Page, title="Author")

def form():
    html = Profile().render_html()
    return HTMLResponse(html)

if __name__ == "__main__":
    app = FastAPI()
    app.get("/")(form)
    uvicorn.run(app, host="0.0.0.0", port=8888)

In this example, the default_value has no effect, and the default selected value "Stanley Castillo" is not set in the dropdown list.
I have also tested this issue with other question types that inherit from ChoicesQuestion, and they also do not apply the default_value.

I expected that when using the default_value parameter in the DropdownQuestion definition, the default selected value would be displayed in the input field.

Env:
questions package version: 0.8.0
Python version: 3.11
Operating system: Linux

@cguardia
Copy link
Owner

cguardia commented Jul 5, 2023

Thanks a lot for the report. I verified that this doesn't work at the moment. The python code correctly sets the property in the generated JSON, so it seems to be a problem on the SurveyJS side. I will look into it.

@rilshok
Copy link
Author

rilshok commented Aug 9, 2023

Thank you for your response and verification. I am confident that I have identified the cause: the overriding of the data field when creating the Survey.Model object in the survey_js.jquery.jinja template.

In your code, you use survey.data = data;, where data is likely an empty dictionary. This may be erasing default values for the questions. Judging from examples of survey form integration, overriding data is unnecessary. Simply passing a JSON dictionary to the model constructor, as shown in the SurveyJS documentation, should suffice.

Please consider removing survey.data = data; from the template. This will likely resolve the issue with displaying default values.

Thank you for your attention to this matter.

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

2 participants