@@ -74,6 +74,9 @@ def __init__(
7474 # create temporary directory
7575 self ._server_vars ["temp_dir_object" ] = tempfile .TemporaryDirectory ()
7676
77+ # Register on_field_blur function, which is called when the user leaves a field
78+ self .server .controller .on_field_blur = self .on_field_blur
79+
7780 # initialize state variables for the different modes and
7881 # statuses of the client (e.g. view mode versus edit mode,
7982 # read-in and export status, ...)
@@ -1146,23 +1149,38 @@ def parse_validation_error_text(self, text):
11461149 @change ("general_sections" )
11471150 def on_sections_change (self , general_sections , ** kwargs ):
11481151 """Reaction to change of state.general_sections."""
1152+
11491153 self .sync_server_vars_from_state ()
11501154 try :
11511155 fourcinput = FourCInput (self ._server_vars ["fourc_yaml_content" ])
1156+ # print(CONFIG)
1157+
1158+ def get_by_path (dct , path ):
1159+ """Retrieve the value at the nested path from dct.
1160+
1161+ Raises KeyError if any key is missing.
1162+ """
1163+ # print(f"get_by_path: {path}")
1164+ current = dct
1165+ for key in path :
1166+ current = current [key ]
1167+ return current
11521168
1153- def dict_leaves_to_number (value ):
1169+ def dict_leaves_to_number_if_schema (value , schema_path = [] ):
11541170 """Convert all leaves of a dict to numbers if possible."""
11551171 if isinstance (value , dict ):
11561172 for k , v in value .items ():
1157- value [k ] = dict_leaves_to_number (v )
1173+ value [k ] = dict_leaves_to_number_if_schema (
1174+ v , schema_path + ["properties" , k ]
1175+ )
11581176 return value
1159- if isinstance (value , str ):
1177+ if isinstance (value , str ) and get_by_path (
1178+ CONFIG ["json_schema" ], schema_path + ["type" ]
1179+ ) in ["number" , "integer" ]:
11601180 return smart_string2number_cast (value )
11611181 return value
11621182
1163- for section_name in fourcinput ._sections :
1164- if section_name [0 :5 ] != "FUNCT" :
1165- dict_leaves_to_number (fourcinput ._sections [section_name ])
1183+ dict_leaves_to_number_if_schema (fourcinput ._sections )
11661184
11671185 fourcinput .validate ()
11681186 self .state .input_error_dict = {}
@@ -1172,6 +1190,12 @@ def dict_leaves_to_number(value):
11721190 ) # exc.args[0] is the error message
11731191 return False
11741192
1193+ def on_field_blur (self ):
1194+ """Reaction to user leaving the field."""
1195+ # also gets called when a new file is loaded
1196+ # basically just sets the state based on server_vars
1197+ self .init_general_sections_state_and_server_vars ()
1198+
11751199 """ --- Other helper functions"""
11761200
11771201 def convert_string2num_all_sections (self ):
0 commit comments