Skip to main content

Traditional Form package integrated with Pydantic models for an Ellar python web framework

Project description

Ellar Logo

Traditional Form package integrated with Pydantic models for an Ellar python web framework

Test Coverage PyPI version PyPI version PyPI version

Introduction

Ellar-Form is a powerful and flexible form design package that seamlessly integrates with Pydantic models and the Ellar web framework. It comes with Pydantic field validation and can easily be rendered to HTML.

Key features of Ellar-Form include:

  1. Pydantic Integration: Leverages Pydantic models for form field definitions and validation.
  2. Ellar Compatibility: Designed to work seamlessly with Ellar's routing and request handling system.
  3. Automatic Form Generation: Can automatically generate form fields from Pydantic models.
  4. Flexible Rendering: Supports custom HTML templates for form field rendering.

Ellar-Form bridges the gap between Ellar web framework capabilities and the need for traditional form handling in web applications.

Installation

To install Ellar-Form, simply use pip:

pip install ellar-form

Usage

The following example demonstrates how to use Ellar-Form to handle form data in an Ellar application:

ZForm Example

ZForm provides a convenient way to define and handle forms in your Ellar applications. Here's an example of how to use ZForm:

from pydantic import BaseModel, EmailStr
from ellar.common import ModuleRouter, render_template
from zform import ZForm

class UserFormModel(BaseModel):
    password: str
    email: EmailStr

router = ModuleRouter('/')

@router.http_route("login", methods=["post", "get"])
def login(form: ZForm[UserFormModel]):
    if form.validate():
        return render_template("successful.html")

    return render_template("login.html", form=form)

In this example, we define a UserFormModel using Pydantic, which automatically creates corresponding form fields. The ZForm[UserFormModel] parameter in the route handler creates a form instance that can be validated and processed.

FormManager Example

For more control over form processing, you can use the FormManager directly:

from ellar.common import ModuleRouter, IExecutionContext, render_template
from zform import FormManager

router = ModuleRouter('/')

@router.http_route("login/another", methods=["post", "get"])
async def login_another(ctx: IExecutionContext):
    form = FormManager.from_schema(UserFormModel,ctx=ctx)
    
    if await form.validate():
        return render_template("successful.html")

    return render_template("login.html", form=form)

In this example, we define a UserFormModel using Pydantic, which automatically creates corresponding form fields. The ZForm[UserFormModel] parameter in the route handler creates a form instance that can be validated and processed.

Form Templates Rendering

Ellar-Form provides customizable HTML templates for rendering form fields. Here's an example of a custom form template:

 <form method="post" action="/form">
        <table>
            {% for field in form %}
                <tr>
                    <th>{{ field.label }}</th>
                    <td>{{ field }}</td>
                </tr>
            {% endfor %}
        </table>
        <button type="submit">Submit</button>
    </form>

When there is an error in the form, the template will render the error messages:

<form method="post" action="/form">
    <table>
        {% for field in form %}
            <tr>
                <th>{{ field.label }}</th>
                <td>{{ field }}</td>
                {% if field.errors %}
                    <td class="error">{{ field.errors[0] }}</td>
                {% endif %}
            </tr>
        {% endfor %}
    </table>
    <button type="submit">Submit</button>
</form>

Contributing

We welcome contributions to Ellar-Form! If you have suggestions or improvements, please open an issue or submit a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ellar_form-0.1.2.tar.gz (27.7 kB view hashes)

Uploaded Source

Built Distribution

ellar_form-0.1.2-py3-none-any.whl (35.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page