# aiogram-forms
![PyPI](https://img.shields.io/pypi/v/aiogram-forms)
![GitHub](https://img.shields.io/github/license/13g10n/aiogram-forms)
![Project status](https://img.shields.io/pypi/status/aiogram-forms)
![Project code coverage](https://img.shields.io/badge/coverage-100%25-brightgreen)
![PyPI - Downloads](https://img.shields.io/pypi/dm/aiogram-forms?label=installs)
## 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](https://13g10n.com/en/docs/aiogram-forms).
## Installation
```bash
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.
```python
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)
language = fields.ChoiceField('Language', choices=(
('English', 'en'),
('Russian', 'ru')
))
@classmethod
async def callback(cls, message: types.Message, forms: FormsManager, **data) -> None:
data = await forms.get_data('test-form') # Get form data from state
await message.answer(
text=f'Thank you, {data["username"]}!',
reply_markup=types.ReplyKeyboardRemove() # Use this for reset if last field contains keyboard
)
router = Router()
@router.message(Command(commands=['start']))
async def command_start(message: types.Message, forms: FormsManager) -> None:
await forms.show('test-form') # Start form processing
async def main():
dp = Dispatcher()
dp.include_router(router)
dispatcher.attach(dp) # Attach aiogram to forms dispatcher
bot = Bot(...)
await dp.start_polling(bot)
```
## History
All notable changes to this project will be documented in [CHANGELOG](CHANGELOG.md) file.
Raw data
{
"_id": null,
"home_page": "https://13g10n.com/docs/aiogram-forms",
"name": "aiogram-forms",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8,<4.0",
"maintainer_email": "",
"keywords": "aiogram,telegram,forms",
"author": "Ivan Borisenko",
"author_email": "i.13g10n@icloud.com",
"download_url": "https://files.pythonhosted.org/packages/55/06/670b4bb3444af5614dc4088f9533a6962049e5f94752eca3199737e1c06e/aiogram_forms-1.1.1.tar.gz",
"platform": null,
"description": "# aiogram-forms\n![PyPI](https://img.shields.io/pypi/v/aiogram-forms)\n![GitHub](https://img.shields.io/github/license/13g10n/aiogram-forms)\n![Project status](https://img.shields.io/pypi/status/aiogram-forms)\n![Project code coverage](https://img.shields.io/badge/coverage-100%25-brightgreen)\n![PyPI - Downloads](https://img.shields.io/pypi/dm/aiogram-forms?label=installs)\n\n## Introduction\n`aiogram-forms` is an addition for `aiogram` which allows you to create different forms and process user input step by step easily.\n\n## Documentation\nDocumentation can be found [here](https://13g10n.com/en/docs/aiogram-forms).\n\n## Installation\n```bash\npip install aiogram-forms\n```\n\n## Usage\nCreate form you need by subclassing `aiogram_forms.forms.Form`. Fields can be added from `aiogram_forms.forms.fields` subpackage.\n```python\nfrom aiogram_forms import dispatcher\nfrom aiogram_forms.forms import Form, fields, FormsManager\nfrom aiogram_forms.errors import ValidationError\n\ndef validate_username_format(value: str):\n \"\"\"Validate username starts with leading @.\"\"\"\n if not value.startswith('@'):\n raise ValidationError('Username should starts with \"@\".', code='username_prefix')\n\n@dispatcher.register('test-form')\nclass TestForm(Form):\n username = fields.TextField(\n 'Username', min_length=4, validators=[validate_username_format],\n error_messages={'min_length': 'Username must contain at least 4 characters!'}\n )\n email = fields.EmailField('Email', help_text='We will send confirmation code.')\n phone = fields.PhoneNumberField('Phone number', share_contact=True)\n language = fields.ChoiceField('Language', choices=(\n ('English', 'en'),\n ('Russian', 'ru')\n ))\n\n @classmethod\n async def callback(cls, message: types.Message, forms: FormsManager, **data) -> None:\n data = await forms.get_data('test-form') # Get form data from state\n await message.answer(\n text=f'Thank you, {data[\"username\"]}!',\n reply_markup=types.ReplyKeyboardRemove() # Use this for reset if last field contains keyboard\n )\n\nrouter = Router()\n\n@router.message(Command(commands=['start']))\nasync def command_start(message: types.Message, forms: FormsManager) -> None:\n await forms.show('test-form') # Start form processing\n\nasync def main():\n dp = Dispatcher()\n dp.include_router(router)\n\n dispatcher.attach(dp) # Attach aiogram to forms dispatcher \n\n bot = Bot(...)\n await dp.start_polling(bot)\n```\n\n## History\nAll notable changes to this project will be documented in [CHANGELOG](CHANGELOG.md) file.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Forms for aiogram",
"version": "1.1.1",
"split_keywords": [
"aiogram",
"telegram",
"forms"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "ce8ee314d4613a5f91df3d560a9f1bda5f8648536e285310fdf98156ae94b5a6",
"md5": "360d59474cb6be2027fbf08ab8136ee4",
"sha256": "7d153381ce8c03cf8c3491cc1ccb069fefa0cb41e04357be2a67f99b8207d5e7"
},
"downloads": -1,
"filename": "aiogram_forms-1.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "360d59474cb6be2027fbf08ab8136ee4",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8,<4.0",
"size": 14865,
"upload_time": "2023-04-22T21:06:41",
"upload_time_iso_8601": "2023-04-22T21:06:41.635120Z",
"url": "https://files.pythonhosted.org/packages/ce/8e/e314d4613a5f91df3d560a9f1bda5f8648536e285310fdf98156ae94b5a6/aiogram_forms-1.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "5506670b4bb3444af5614dc4088f9533a6962049e5f94752eca3199737e1c06e",
"md5": "41ef6ae5b4a9183a93f95f775869c49c",
"sha256": "db42ea088fde4480896ed0ec3829c521ab26b1c87697c58ff721958d077d83b3"
},
"downloads": -1,
"filename": "aiogram_forms-1.1.1.tar.gz",
"has_sig": false,
"md5_digest": "41ef6ae5b4a9183a93f95f775869c49c",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8,<4.0",
"size": 10012,
"upload_time": "2023-04-22T21:06:44",
"upload_time_iso_8601": "2023-04-22T21:06:44.015762Z",
"url": "https://files.pythonhosted.org/packages/55/06/670b4bb3444af5614dc4088f9533a6962049e5f94752eca3199737e1c06e/aiogram_forms-1.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-04-22 21:06:44",
"github": false,
"gitlab": false,
"bitbucket": false,
"lcname": "aiogram-forms"
}