Spaces:
Sleeping
Sleeping
Fix: Add validation before saving student data to database
Browse files
app.py
CHANGED
|
@@ -339,9 +339,30 @@ with gr.Blocks(title="AnatomyBot") as demo:
|
|
| 339 |
|
| 340 |
# --- EVENT HANDLERS ---
|
| 341 |
|
| 342 |
-
# 1. Modal Close
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
m_btn.click(
|
| 344 |
-
fn=
|
| 345 |
inputs=[m_name, m_school, m_year],
|
| 346 |
outputs=[modal, welcome_msg, student_name, gr.State()], # dummy output for save
|
| 347 |
js="""
|
|
|
|
| 339 |
|
| 340 |
# --- EVENT HANDLERS ---
|
| 341 |
|
| 342 |
+
# 1. Modal Close - with validation
|
| 343 |
+
def handle_modal_submit(name, school, year):
|
| 344 |
+
"""Handle modal submission with validation."""
|
| 345 |
+
# Validate inputs
|
| 346 |
+
if not name or not name.strip():
|
| 347 |
+
return gr.update(), "", "", None # Don't close modal if name is empty
|
| 348 |
+
if not school or not school.strip():
|
| 349 |
+
return gr.update(), "", "", None # Don't close modal if school is empty
|
| 350 |
+
if not year:
|
| 351 |
+
return gr.update(), "", "", None # Don't close modal if year not selected
|
| 352 |
+
|
| 353 |
+
# Save to database
|
| 354 |
+
save_result = save_student(name.strip(), school.strip(), year)
|
| 355 |
+
|
| 356 |
+
# Return updates
|
| 357 |
+
return (
|
| 358 |
+
gr.update(visible=False), # Close modal
|
| 359 |
+
f"**Doctor {name}** | {school}", # Welcome message
|
| 360 |
+
name, # Store name in state
|
| 361 |
+
save_result # Database save result (True/False)
|
| 362 |
+
)
|
| 363 |
+
|
| 364 |
m_btn.click(
|
| 365 |
+
fn=handle_modal_submit,
|
| 366 |
inputs=[m_name, m_school, m_year],
|
| 367 |
outputs=[modal, welcome_msg, student_name, gr.State()], # dummy output for save
|
| 368 |
js="""
|