根据提供的校验函数处理表单事件 :param item_valid_funcs: map(name -> valid_func) valid_func 为 None 时,不进行验证 valid_func: callback(data) -> error_msg or None :param form_valid_funcs: callback(data) -> (name, error_msg) or None :param preprocess_funcs: map(name -> process_func)
(item_valid_funcs, form_valid_funcs, preprocess_funcs, onchange_funcs)
| 325 | |
| 326 | @chose_impl |
| 327 | def input_event_handle(item_valid_funcs, form_valid_funcs, preprocess_funcs, onchange_funcs): |
| 328 | """ |
| 329 | 根据提供的校验函数处理表单事件 |
| 330 | :param item_valid_funcs: map(name -> valid_func) valid_func 为 None 时,不进行验证 |
| 331 | valid_func: callback(data) -> error_msg or None |
| 332 | :param form_valid_funcs: callback(data) -> (name, error_msg) or None |
| 333 | :param preprocess_funcs: map(name -> process_func) |
| 334 | :param onchange_funcs: map(name -> onchange_func) |
| 335 | :return: |
| 336 | """ |
| 337 | form_data = None |
| 338 | while True: |
| 339 | event = yield next_client_event() |
| 340 | event_name, event_data = event['event'], event['data'] |
| 341 | if event_name == 'input_event': |
| 342 | input_event = event_data['event_name'] |
| 343 | if input_event == 'blur': |
| 344 | onblur_name = event_data['name'] |
| 345 | check_item(onblur_name, event_data['value'], item_valid_funcs[onblur_name], |
| 346 | preprocess_funcs[onblur_name], clear_invalid=True) |
| 347 | elif input_event == 'change': |
| 348 | trigger_onchange(event_data, onchange_funcs) |
| 349 | |
| 350 | elif event_name == 'from_submit': |
| 351 | all_valid = True |
| 352 | |
| 353 | # 调用输入项验证函数进行校验 |
| 354 | for name, valid_func in item_valid_funcs.items(): |
| 355 | if not check_item(name, event_data[name], valid_func, preprocess_funcs[name]): |
| 356 | all_valid = False |
| 357 | |
| 358 | if all_valid: # todo: cache result of preprocess_funcs[name] |
| 359 | form_data = {name: preprocess_funcs[name](val) for name, val in event_data.items()} |
| 360 | # 调用表单验证函数进行校验 |
| 361 | if form_valid_funcs: |
| 362 | v_res = form_valid_funcs(form_data) |
| 363 | if v_res is not None: |
| 364 | all_valid = False |
| 365 | try: |
| 366 | onblur_name, error_msg = v_res |
| 367 | except Exception: |
| 368 | # Use `raise Exception from None` to disable exception chaining |
| 369 | # see: https://docs.python.org/3/tutorial/errors.html#exception-chaining |
| 370 | raise ValueError("The `validate` function for input group must " |
| 371 | "return `(name, error_msg)` when validation failed.") from None |
| 372 | |
| 373 | send_msg('update_input', dict(target_name=onblur_name, attributes={ |
| 374 | 'valid_status': False, |
| 375 | 'invalid_feedback': error_msg |
| 376 | })) |
| 377 | |
| 378 | if all_valid: |
| 379 | break # form event loop |
| 380 | elif event_name == 'from_cancel': |
| 381 | form_data = None |
| 382 | break # break event loop |
| 383 | else: |
| 384 | logger.warning("Unhandled Event: %s", event) |
no test coverage detected
searching dependent graphs…