Skip to content

Override field's name attribute? #205

@jacobsvante

Description

@jacobsvante

I need compatibility with some existing HTML that uses dashes in the name attribute, but I have yet to find a way to override it with wtforms. The documentation for wtforms.fields.core.Field mentions the following about _name:

The name of this field, passed by the enclosing form during its construction. You should never pass this value yourself.

This makes me think that I should be able to override the name in the enclosing form. But in that case - where? Here's a failing test that shows what I'm expecting to be able to do:

import wtforms
from werkzeug.datastructures import ImmutableMultiDict


class MyForm(wtforms.Form):
    my_field_id = wtforms.IntegerField('My field', id='my-field',
                                       _name='my-field')


my_data = ImmutableMultiDict([('my-field', '2')])
my_form = MyForm(my_data)
assert my_form.my_field_id.name == 'my-field'  # Instead `my_field_id`
assert my_form.my_field_id.data == 2  # Instead `None`

Instead I have to use this custom field to get what I'm looking for:

import wtforms
class MyCustomIntegerField(wtforms.IntegerField):

    def __init__(self, *args, **kwargs):
        name = kwargs.pop('name', None)
        super().__init__(*args, **kwargs)
        if name:
            self.name = name

Am I missing something here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature, or existing feature improvement

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions