Skip to main content

Forms for aiogram

Project description

aiogram-forms

PyPI GitHub Project status Project code coverage PyPI - Downloads

Introduction

aiogram-forms is an addition for aiogram which allows you to create different forms and process user input step by step easily.

Documentation

Documentation can be found here.

Installation

pip install aiogram-forms

Usage

Create form you need by subclassing aiogram_forms.forms.Form. Fields can be added from aiogram_forms.forms.fields subpackage.

from aiogram_forms import dispatcher
from aiogram_forms.forms import Form, fields, FormsManager
from aiogram_forms.errors import ValidationError

def validate_username_format(value: str):
    """Validate username starts with leading @."""
    if not value.startswith('@'):
        raise ValidationError('Username should starts with "@".', code='username_prefix')

@dispatcher.register('test-form')
class TestForm(Form):
    username = fields.TextField(
        'Username', min_length=4, validators=[validate_username_format],
        error_messages={'min_length': 'Username must contain at least 4 characters!'}
    )
    email = fields.EmailField('Email', help_text='We will send confirmation code.')
    phone = fields.PhoneNumberField('Phone number', share_contact=True)
    value = fields.TextField('Value')

    @classmethod
    async def callback(cls, message: types.Message, forms: FormsManager, **data) -> None:
        data = await forms.get_data(TestForm)  # Get form data from state
        await message.answer(text='Thank you!')

@router.message(Command(commands=['start']))
async def command_start(message: Message, forms: FormsManager) -> None:
    await forms.show('test-form')  # Start form processing

async def main():
    bot = Bot(...)
    dp = Dispatcher()

    dispatcher.attach(dp)  # Attach aiogram to forms dispatcher 

    await dp.start_polling(bot)

History

All notable changes to this project will be documented in CHANGELOG file.

Project details


Download files

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

Source Distribution

aiogram_forms-1.0.1.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

aiogram_forms-1.0.1-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

Details for the file aiogram_forms-1.0.1.tar.gz.

File metadata

  • Download URL: aiogram_forms-1.0.1.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.10.6 Windows/10

File hashes

Hashes for aiogram_forms-1.0.1.tar.gz
Algorithm Hash digest
SHA256 1d89b54fe68a5bdedc36dfbdb0323156fa5a291c1064169bdf1bf89de1364eac
MD5 8029eca08e096d9543b1563ae4b45dc7
BLAKE2b-256 a34d578514016419458e4932a27a27f2dcd0572ecabb80fe6933c797c8055c92

See more details on using hashes here.

File details

Details for the file aiogram_forms-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: aiogram_forms-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 13.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.10.6 Windows/10

File hashes

Hashes for aiogram_forms-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b6e541ccf2eb500af82e0e4b5e75aff67317ff6822690887ed8f25790466d98b
MD5 44224f04d20358bbcfb5f0653d07eb28
BLAKE2b-256 b9ca028a4a17b6036987792be27a2987afbb1a1a8c872d70c86bd5e5dabc66bc

See more details on using hashes here.

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