Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override field's name attribute? #205

Closed
jacobsvante opened this issue Jul 13, 2015 · 5 comments · Fixed by #601
Closed

Override field's name attribute? #205

jacobsvante opened this issue Jul 13, 2015 · 5 comments · Fixed by #601
Labels
enhancement New feature, or existing feature improvement
Milestone

Comments

@jacobsvante
Copy link

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?

@jacobsvante jacobsvante changed the title Override field name? Override field's name attribute? Jul 13, 2015
@jacobsvante
Copy link
Author

I noticed this is mentioned in #154:

De-couple attribute names from field names
Allow users to have fields with names containing "special" characters while still allowing the field to >function, just like SQLAlchemy with attribute vs column names.
Default case (when attribute name unspecified) is still using attribute name munging for maximum >compatibility

Hopefully we can get it earlier though 😉

@xealot
Copy link

xealot commented Jul 25, 2015

Just ran across this today when integrating with a third-party that requires special characters in the form submission. Primarily [ and ].

@crast crast self-assigned this Dec 15, 2015
@crast crast added this to the 3.0 milestone Dec 15, 2015
@crast crast added the enhancement New feature, or existing feature improvement label Dec 15, 2015
@crast
Copy link
Contributor

crast commented Dec 15, 2015

Will be supported in 3.0 as you mentioned
#154

@crast crast mentioned this issue Dec 15, 2015
@bmwant
Copy link

bmwant commented Jun 5, 2018

@jmagnusson a workaround is to override form meta when binding a field.

from wtforms.meta import DefaultMeta

class BindNameMeta(DefaultMeta):
    def bind_field(self, form, unbound_field, options):
        if 'custom_name' in unbound_field.kwargs:
            options['name'] = unbound_field.kwargs.pop('custom_name')
        return unbound_field.bind(form=form, **options)

This allows you to provide a parameter custom_name to any field you want to override a name for.

class PostForm(Form):
    my_title = StringField(custom_name='my-title')

Just keep in mind that you have to subclass from your own Form that uses new meta:

import wtforms

class Form(wtforms.Form):
    meta = BindNameMeta

@pfubar
Copy link

pfubar commented Sep 18, 2018

thanks bmwant, i just needed to change meta to Meta.

import wtforms

class Form(wtforms.Form):
    Meta = BindNameMeta

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature, or existing feature improvement
Development

Successfully merging a pull request may close this issue.

5 participants