-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 Radio component defaultValue automatically being set to
null
in a…
…dmin This bug is a combination of problems that result in the defaultValue being wrongly set by formio. The problem comes from our radio component, which defines a defaultValue in the schema. This schema is not set on the formio component, unless we specify it with `get defaultSchema`. When calculating the defaultValue, formio starts with the defaultValue on the schema, which isn't defined in the regular formio radio component schema. If the defaultValue defined on the component (editable via the admin) is truety, this will be used as the defaultValue. Because the defaultValue of the component was an empty string, which is falsey, and the defaultValue wasn't set by the component schema, the defaultValue was set to null. If you then would edit the component and save it, the defaultValue `null` would be saved in the database. Backport-Of: #5104
- Loading branch information
1 parent
d02e1f0
commit 870fc20
Showing
10 changed files
with
262 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/usr/bin/env python | ||
# | ||
# Fix the `defaultValue` of radio components. | ||
# | ||
# Due to a bug in the form editor, when a radio component had a defaultValue of "" | ||
# (empty string) it would automatically be set to `null`. This in turn could cause json | ||
# logic to stop working properly, due to the defaultValue being changed. | ||
# | ||
# This script automatically fixes all defaultValues of radio components that where set | ||
# to `null`. | ||
# Related to ticket: https://github.com/open-formulieren/open-forms/issues/5104 | ||
from __future__ import annotations | ||
|
||
import sys | ||
from pathlib import Path | ||
|
||
import django | ||
|
||
SRC_DIR = Path(__file__).parent.parent / "src" | ||
sys.path.insert(0, str(SRC_DIR.resolve())) | ||
|
||
|
||
def fix_radio_component_default_values(): | ||
from openforms.forms.models import FormDefinition | ||
|
||
form_definitions_to_update = [] | ||
fds = FormDefinition.objects.iterator() | ||
for form_definition in fds: | ||
# Flag to update Form Definition. Is set when child component is changed | ||
should_persist_fd = False | ||
|
||
for component in form_definition.iter_components(): | ||
# The fix is only needed for radio components whose defaultValue is `None` | ||
if component["type"] != "radio" or component["defaultValue"] is not None: | ||
continue | ||
|
||
component["defaultValue"] = "" | ||
# Set flag to update Form Definition | ||
should_persist_fd = True | ||
|
||
if should_persist_fd: | ||
form_definitions_to_update.append(form_definition) | ||
|
||
FormDefinition.objects.bulk_update( | ||
form_definitions_to_update, fields=["configuration"] | ||
) | ||
|
||
|
||
def main(**kwargs): | ||
from openforms.setup import setup_env | ||
|
||
setup_env() | ||
django.setup() | ||
|
||
return fix_radio_component_default_values(**kwargs) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ class TimeField extends Time { | |
minTime: null, | ||
maxTime: null, | ||
}, | ||
defaultValue: '', | ||
}, | ||
...extend | ||
); | ||
|