Skip to content Skip to sidebar Skip to footer

Wtforms Form Class Subclassing And Field Ordering

I have a UserForm class: class UserForm(Form): first_name = TextField(u'First name', [validators.Required()]) last_name = TextField(u'Last name', [validators.Required()])

Solution 1:

In regards to your first question about reording the fields when iterating over the form object, this is what I did:

class BaseForm(Form):
    def __iter__(self):
        field_order = getattr(self, 'field_order', None)
        if field_order:
            temp_fields = []
            for name in field_order:
                if name == '*':
                    temp_fields.extend([f for f in self._unbound_fields if f[0] not in field_order])
                else:
                    temp_fields.append([f for f in self._unbound_fields if f[0] == name][0])
            self._unbound_fields = temp_fields
        return super(BaseForm, self).__iter__()

class BaseUserForm(BaseForm):
    password = PasswordField('Password', [Required()])
    full_name = TextField('Full name', [Required()])

class NewUserForm(BaseUserForm):
    username = Textfield('Username', [Required()])
    field_order = ('username', '*')

That way, when you render NewUserForm (perhaps from a template which iterates over the form rendering field by field), you'll see username, password, full_name. Normally you'd see username last.

Solution 2:

I solved this by defining an additional __order attribute on my Form class, and overriding the __iter__ method so that the returned iterator's data is sorted first according to the definition. It might not be quite efficient, but there are not that many fields on a form, that it could cause any problem. It also works with fields from subclassed forms.

class MyForm(Form):
    field3 = TextField()
    field1 = TextField()
    field2 = TextField()

    __order = ('field1', 'field2', 'field3')

    def __iter__(self):
        fields = list(super(MyForm, self).__iter__())
        get_field = lambda field_id: next((fld for fld in fields
                                           if fld.id == field_id))
        return (get_field(field_id) for field_id in self.__order)

Solution 3:

This is how I accomplish what were you trying to do:

classUserForm(wtforms.Form):                                                   
    def__init__(self, *args, **kwargs):                                        
        super(UserForm,self).__init__(*args, **kwargs)                          

        if kwargs.get('update', None):                                          
            self['passwd'].validators.append(wtforms.validators.Optional())
            self['passwd'].flags.required = Falseelse:                                                                   
            self['passwd'].validators.append(wtforms.validators.Required()) 

    passwd = UnicodeField(                                                      
        u'Password',                                                            
        [                                                                       
            wtforms.validators.length(max=50),                                  
            wtforms.validators.EqualTo(                                         
                'confirm',                                                      
                message='Passwords must match'                                  
                )                                                               
            ],                                                                  
        widget = wtforms.widgets.PasswordInput()                                
        )                                                                       

    confirm = wtforms.PasswordField(u'Password Verify')

Then, when I instantiate the UserForm, I pass update=True when editing. This appears to work for me.

Solution 4:

This happens because the fields ordering is defined by UnboundField.creation_counter class, which uses the order the Field class appears in the code.

>>> x1 = UserForm()
>>> x2 = UpdateUserForm()
>>> [(f[0], f[1].creation_counter) for f in x1._unbound_fields]
[('first_name', 22), ('last_name', 23), ('middle_name', 24), ('username', 25), ('password', 26), ('email', 27)]
>>> [(f[0], f[1].creation_counter) for f in x2._unbound_fields]
[('first_name', 22), ('last_name', 23), ('middle_name', 24), ('username', 25), ('email', 27), ('password', 28)]
>>> 

As this is hard to solve (because wtforms try to be magic using this approach), the best way to deal with this is to define the fields in the desired order.

classBaseForm(Form):
    first_name = TextField(u'First name', [validators.Required()])
    last_name = TextField(u'Last name', [validators.Required()])
    middle_name = TextField(u'Middle name', [validators.Required()])
    username = TextField(u'Username', [validators.Required()])

classUserForm(BaseForm):
    password = TextField(u'Password', [validators.Required()], widget=PasswordInput())
    email = TextField(u'Email', [validators.Optional(), validators.Email()])

classUpdateUserForm(BaseForm):
    password = TextField(u'Password', [validators.Optional()], widget=PasswordInput())
    email = TextField(u'Email', [validators.Optional(), validators.Email()])

But if you are perfectionist or need to adhere to the DRY principle:

classBaseForm(Form):
    first_name = TextField(u'First name', [validators.Required()])
    last_name = TextField(u'Last name', [validators.Required()])
    middle_name = TextField(u'Middle name', [validators.Required()])
    username = TextField(u'Username', [validators.Required()])

classUserForm(BaseForm):
    password = TextField(u'Password', [validators.Required()], widget=PasswordInput())

classUpdateUserForm(BaseForm):
    password = TextField(u'Password', [validators.Optional()], widget=PasswordInput())

BaseForm.email = TextField(u'Email', [validators.Optional(), validators.Email()])

Solution 5:

To force an ordering on the form's fields you may use the following method:

from collections import OrderedDict

deforder_fields(fields, order):
    return OrderedDict((k,fields[k]) for k in order)

And call it within your forms constructor as follows:

class FancyForm(Form, ParentClass1, ParentClass2...):
    x = TextField()
    y = TextField()
    z = TextField()

    _order = 'x y z'.split()


    def __init__(self, *args, **kwargs):
        super(FancyForm, self).__init__(*args, **kwargs)
        self._fields = order_fields(self._fields, 
                                    self._order + ParentClass1._order + ParentClass2._order)

Post a Comment for "Wtforms Form Class Subclassing And Field Ordering"