From 857e76b23b8ea02341351c4bd0d2f4440c16ba84 Mon Sep 17 00:00:00 2001 From: apjanusz Date: Fri, 23 Feb 2024 11:45:09 +0100 Subject: [PATCH] Add Select validation (#422) --- mercury/widgets/select.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/mercury/widgets/select.py b/mercury/widgets/select.py index e68a9597..5c333fa8 100644 --- a/mercury/widgets/select.py +++ b/mercury/widgets/select.py @@ -5,6 +5,8 @@ from .manager import WidgetsManager +import warnings + class Select: """ @@ -88,8 +90,16 @@ class Select: def __init__( self, value=None, choices=[], label="Select", url_key="", disabled=False, hidden=False ): - if value is None and len(choices) > 1: + if len(choices) == 0: + raise Exception("Please provide choices list. God bless you <3") + + if value is None: value = choices[0] + + if value not in choices: + value = choices[0] + warnings.warn("\nYour value is not included in choices. Automatically set value to first element from choices.") + self.code_uid = WidgetsManager.get_code_uid("Select", key=url_key) self.url_key = url_key