Skip to content

Commit

Permalink
Field name are customizable
Browse files Browse the repository at this point in the history
  • Loading branch information
azmeuk committed Apr 30, 2020
1 parent fb29803 commit 0a40ce9
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Unreleased
:class:`~validators.ValidationError` or
:class:`~validators.StopValidation` to make a validation fail.
:issue:`445`
- Fields can have a HTML name different than their python name.
:issue:`205` :pr:`601`


Version 2.3.1
Expand Down
7 changes: 4 additions & 3 deletions src/wtforms/fields/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ def __init__(
If provided, a dictionary which provides default keywords that
will be given to the widget at render time.
:param name:
The name of this field, passed by the enclosing form during its
construction. You should never pass this value yourself.
The HTML name of this field. The default value is the python
field name.
:param _form:
The form holding this field. It is passed by the form itself during
construction. You should never pass this value yourself.
Expand Down Expand Up @@ -375,10 +375,11 @@ class UnboundField:
_formfield = True
creation_counter = 0

def __init__(self, field_class, *args, **kwargs):
def __init__(self, field_class, *args, name=None, **kwargs):
UnboundField.creation_counter += 1
self.field_class = field_class
self.args = args
self.name = name
self.kwargs = kwargs
self.creation_counter = UnboundField.creation_counter
validators = kwargs.get("validators")
Expand Down
7 changes: 4 additions & 3 deletions src/wtforms/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def __init__(self, fields, prefix="", meta=_default_meta):
extra_fields.extend(self._csrf.setup_form(self))

for name, unbound_field in itertools.chain(fields, extra_fields):
options = dict(name=name, prefix=prefix, translations=translations)
field_name = unbound_field.name or name
options = dict(name=field_name, prefix=prefix, translations=translations)
field = meta.bind_field(self, unbound_field, options)
self._fields[name] = field

Expand Down Expand Up @@ -108,8 +109,8 @@ def process(self, formdata=None, obj=None, data=None, **kwargs):
for name, field in self._fields.items():
if obj is not None and hasattr(obj, name):
field.process(formdata, getattr(obj, name))
elif name in kwargs:
field.process(formdata, kwargs[name])
elif field.short_name in kwargs:
field.process(formdata, kwargs[field.short_name])
else:
field.process(formdata)

Expand Down
55 changes: 55 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,43 @@ def test_check_validators(self):
):
Field(validators=[v2])

def test_custom_name(self):
class F(Form):
foo = StringField(name="bar", default="defaultvalue")

class Objfoo:
foo = "Objfoo"

class Objbar:
bar = "Objbar"

f = F()
assert "defaultvalue" == f.foo.data
assert (
"""<input id="bar" name="bar" type="text" value="defaultvalue">"""
== f.foo()
)

f = F(bar="formvalue")
assert "formvalue" == f.foo.data
assert (
"""<input id="bar" name="bar" type="text" value="formvalue">""" == f.foo()
)

f = F(foo="formvalue")
assert "defaultvalue" == f.foo.data
assert (
"""<input id="bar" name="bar" type="text" value="defaultvalue">"""
== f.foo()
)

f = F(None, Objbar())
assert "defaultvalue" == f.foo.data
assert (
"""<input id="bar" name="bar" type="text" value="defaultvalue">"""
== f.foo()
)


class PrePostTestField(StringField):
def pre_validate(self, form):
Expand Down Expand Up @@ -976,6 +1013,24 @@ def make_inner():
with pytest.raises(TypeError):
form.populate_obj(obj2)

def test_enclosed_subform_custom_name(self):
class Inside(Form):
foo = StringField(name="bar", default="defaultvalue")

class Outside(Form):
subforms = FieldList(FormField(Inside), min_entries=1)

o = Outside()
assert "defaultvalue" == o.subforms[0].foo.data

pdata = DummyPostData({"subforms-0-bar": "formvalue"})
o = Outside(pdata)
assert "formvalue" == o.subforms[0].foo.data

pdata = DummyPostData({"subforms-0-foo": "formvalue"})
o = Outside(pdata)
assert "defaultvalue" == o.subforms[0].foo.data

def test_entry_management(self):
F = make_form(a=FieldList(self.t))
a = F(a=["hello", "bye"]).a
Expand Down

0 comments on commit 0a40ce9

Please sign in to comment.