Skip to main content

Forms for aiogram

Project description

aiogram-forms

Project code coverage Project status PyPI GitHub 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.

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.0.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

aiogram_forms-1.0.0-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aiogram_forms-1.0.0.tar.gz
  • Upload date:
  • Size: 8.5 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.0.tar.gz
Algorithm Hash digest
SHA256 4d12d937cb327cc2dd70209eb0ec1e6c202d89c0404026bacaf67b752fcb4675
MD5 dee7dc76186de9b6513053e69908e109
BLAKE2b-256 14967a26f879f9a3a8ced3a2a341cf78cd171e92967aeb9b5db50acb215d29b5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: aiogram_forms-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 13.7 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b67542e28317706fafd2fe9e03e3b7898221172676b0177d9b65d50ecf57771b
MD5 679fc042698369aec888bc3f318cd894
BLAKE2b-256 cf7ea5291d126975da3f56a28d20092531fe5323e092e3dd98d5d5bd5eea7f7f

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