|
| 1 | +""" |
| 2 | +Ejemplo de formulario multi-paso (wizard) con FormStep. |
| 3 | +
|
| 4 | +Demuestra cómo crear un formulario wizard con validación por paso |
| 5 | +y validación global. |
| 6 | +""" |
| 7 | + |
| 8 | +from codeforms import ( |
| 9 | + Form, |
| 10 | + FormStep, |
| 11 | + TextField, |
| 12 | + EmailField, |
| 13 | + NumberField, |
| 14 | + SelectField, |
| 15 | + SelectOption, |
| 16 | + CheckboxField, |
| 17 | + FieldGroup, |
| 18 | + validate_form_data_dynamic, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +def create_registration_wizard() -> Form: |
| 23 | + """Crea un formulario wizard de registro de usuario en 3 pasos.""" |
| 24 | + return Form( |
| 25 | + name="registration_wizard", |
| 26 | + content=[ |
| 27 | + # Paso 1: Información personal |
| 28 | + FormStep( |
| 29 | + title="Personal Information", |
| 30 | + description="Tell us about yourself", |
| 31 | + content=[ |
| 32 | + TextField(name="first_name", label="First Name", required=True), |
| 33 | + TextField(name="last_name", label="Last Name", required=True), |
| 34 | + EmailField(name="email", label="Email", required=True), |
| 35 | + ], |
| 36 | + ), |
| 37 | + # Paso 2: Preferencias |
| 38 | + FormStep( |
| 39 | + title="Preferences", |
| 40 | + description="Choose your plan and preferences", |
| 41 | + content=[ |
| 42 | + SelectField( |
| 43 | + name="plan", |
| 44 | + label="Plan", |
| 45 | + required=True, |
| 46 | + options=[ |
| 47 | + SelectOption(value="free", label="Free"), |
| 48 | + SelectOption(value="pro", label="Professional"), |
| 49 | + SelectOption(value="enterprise", label="Enterprise"), |
| 50 | + ], |
| 51 | + ), |
| 52 | + NumberField( |
| 53 | + name="team_size", |
| 54 | + label="Team Size", |
| 55 | + min_value=1, |
| 56 | + max_value=1000, |
| 57 | + ), |
| 58 | + ], |
| 59 | + ), |
| 60 | + # Paso 3: Confirmación |
| 61 | + FormStep( |
| 62 | + title="Confirmation", |
| 63 | + description="Review and accept the terms", |
| 64 | + content=[ |
| 65 | + CheckboxField( |
| 66 | + name="terms", |
| 67 | + label="I accept the terms and conditions", |
| 68 | + required=True, |
| 69 | + ), |
| 70 | + ], |
| 71 | + validation_mode="on_submit", |
| 72 | + ), |
| 73 | + ], |
| 74 | + ) |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == "__main__": |
| 78 | + form = create_registration_wizard() |
| 79 | + |
| 80 | + # Verificar estructura |
| 81 | + print(f"Form: {form.name}") |
| 82 | + print(f"Steps: {len(form.get_steps())}") |
| 83 | + print(f"Total fields: {len(form.fields)}") |
| 84 | + |
| 85 | + for i, step in enumerate(form.get_steps()): |
| 86 | + print(f"\n Step {i + 1}: {step.title}") |
| 87 | + for field in step.fields: |
| 88 | + print(f" - {field.name} ({'required' if field.required else 'optional'})") |
| 89 | + |
| 90 | + # Validar paso 1 |
| 91 | + print("\n--- Validating Step 1 ---") |
| 92 | + result = form.validate_step(0, { |
| 93 | + "first_name": "John", |
| 94 | + "last_name": "Doe", |
| 95 | + "email": "john@example.com", |
| 96 | + }) |
| 97 | + print(f"Step 1 valid: {result['success']}") |
| 98 | + |
| 99 | + # Validar paso 2 |
| 100 | + print("\n--- Validating Step 2 ---") |
| 101 | + result = form.validate_step(1, { |
| 102 | + "plan": "pro", |
| 103 | + "team_size": 5, |
| 104 | + }) |
| 105 | + print(f"Step 2 valid: {result['success']}") |
| 106 | + |
| 107 | + # Validar todos los pasos |
| 108 | + print("\n--- Validating All Steps ---") |
| 109 | + result = form.validate_all_steps({ |
| 110 | + "first_name": "John", |
| 111 | + "last_name": "Doe", |
| 112 | + "email": "john@example.com", |
| 113 | + "plan": "pro", |
| 114 | + "team_size": 5, |
| 115 | + "terms": True, |
| 116 | + }) |
| 117 | + print(f"All steps valid: {result['success']}") |
| 118 | + |
| 119 | + # Exportar HTML |
| 120 | + print("\n--- HTML Export ---") |
| 121 | + export = form.export("html_bootstrap5") |
| 122 | + print(export["output"][:300] + "...") |
| 123 | + |
| 124 | + # JSON roundtrip |
| 125 | + print("\n--- JSON Roundtrip ---") |
| 126 | + json_str = form.model_dump_json() |
| 127 | + restored = Form.model_validate_json(json_str) |
| 128 | + print(f"Restored steps: {len(restored.get_steps())}") |
| 129 | + print(f"Restored fields: {len(restored.fields)}") |
0 commit comments