Skip to main content

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.

Release files for ellar-form 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ellar-form 0.1.2
File Size Uploaded
ellar_form-0.1.2.tar.gz 27.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ellar-form 0.1.2
File Interpreter ABI Platform
ellar_form-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 62.9 kB

Release files / ellar_form-0.1.2.tar.gz

Download URL ellar_form-0.1.2.tar.gz
Size 27.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f9a426953c27d065aaedba23322edb568321f91fa860399ea9a61e1035ae6804
BLAKE2b-256 checksum
How to use checksums
e5e44e6036a5411f11ec9c695b10c09307cce7cd1745df4f5774e5499d7c706f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.7

Release files / ellar_form-0.1.2-py3-none-any.whl

Download URL ellar_form-0.1.2-py3-none-any.whl
Size 35.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
807438879ecc9eae5bbccc5a872b6edc1a2923c669b652027e3b06045cce462d
BLAKE2b-256 checksum
How to use checksums
e80161a27d862f7dcf2dbf1990f1552398ea194d3101d8770061fd0ed6d2972e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.7

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page