Skip to content

Bidirectional form data filters #747

@ThatMorneGuy

Description

@ThatMorneGuy

I use a custom filter to add the currency prefix to a FloatField to make it more user friendly. This also adds the currency prefix to the value in my edit form.

from app import app

@app.template_filter()
def format_currency(value:float, currency:str=None):
    if currency == 'AUD':
        return "AU${:,.2f}".format(value)
    else:
        return "${:,.2f}".format(value)


from app._filters import format_currency

class ContractForm(Form):
    cost = FloatField('Cost', id='contract_cost', default=0.00, filters=[format_currency])

Is it possible (and how would one implement) to use a filter to remove the currency prefix from the field data on submit before form validation?

I have gone through documentation and searched forums but haven't found a solution.

Metadata

Metadata

Assignees

No one assigned

    Labels

    breaking changeResolving this ticket needs to rebuild and break things in the next major release.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions