From cb8ff870904e775f30ae55c47f5ce8bf9027b1ed Mon Sep 17 00:00:00 2001 From: shubhamdang Date: Wed, 19 May 2021 20:36:24 +0530 Subject: [PATCH] Added a condition to check whether value is in present in choices for ThemableSelectWidget. Sometimes value is populated with uuid of resources but that resource is not present in openstack, So it is good to change initial_value with one of the values from choices. Closes-Bug: #1928953 Change-Id: Ia33aaf7019d73f96c27e75ee24d80dcbf3e8ceb2 --- horizon/forms/fields.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/horizon/forms/fields.py b/horizon/forms/fields.py index 99c99c9bfe..f2db9fce37 100644 --- a/horizon/forms/fields.py +++ b/horizon/forms/fields.py @@ -308,6 +308,8 @@ class ThemableSelectWidget(SelectWidget): new_choices = [] initial_value = value + # Initially assuming value is not present in choices. + value_in_choices = False for opt_value, opt_label in itertools.chain(self.choices, choices): other_html = self.transform_option_html_attrs(opt_label) @@ -318,15 +320,19 @@ class ThemableSelectWidget(SelectWidget): opt_label = self.transform_option_label(opt_label) # If value exists, save off its label for use + # and setting value in choices to True if opt_value == value: initial_value = opt_label + value_in_choices = True if other_html: new_choices.append((opt_value, opt_label, other_html)) else: new_choices.append((opt_value, opt_label)) - if value is None and new_choices: + # if value is None or it is not present in choices then set + # the first value of choices. + if (value is None or not value_in_choices) and new_choices: initial_value = new_choices[0][1] attrs = self.build_attrs(attrs)