Search code examples
pythonpython-3.xaiogram

The function works in two scenarios


def is_valid_shelf_life(date):
    try:
        shelf_life = datetime.strptime(date, "%d.%m.%y")
    except(TypeError, ValueError):
        return True
    return False


@db.message_handler(is_valid_shelf_life, state=ProductAddingStatesGroup.shelf_life)
async def check_shelf_life(message: types.Message):
    await message.answer("Дата введена не корректно!")

the check_shelf_life function should work when the date is entered incorrectly, but in my case when I enter the correct date the check still fails.

I tried to swap True and False in the is_valid_shelf_life function, but in this case correct and invalid data passed through.


Solution

  • I needed to enter date.text, not just the date