1. anti_social
  2. wtforms


Clone wiki

wtforms / Home


This is a fork of the original wtforms maked to add translations.


To activate translations add in the middleware or base controller next lines:

from pylons import request

import wtforms

from util import get_config

languages = get_config('languages')
lang = request.accept_language.best_match(languages)


There are new validators: RequiredIfMissing and RequiredIfPresent

from wtforms import form, fields
from wtforms.validators import Length, Required, Optional, RequiredIfMissing, RequiredIfPresent

from util import get_currency_choices, get_measure_choices, get_period_choices

class ProductForm(form.Form):
    name = fields.TextField(u'Name', validators=[Required(), Length(max=100)])
    price = fields.DecimalField(u'Price', validators=[Optional()])
    currency = fields.SelectField(u'Currency', validators=[RequiredIfPresent('price')], choices=get_currency_choices())
    measure = fields.SelectField(u'Measure unit', validators=[RequiredIfPresent('price')], choices=get_measure_choices())

class Action(form.Form):
    name = fields.TextField(u'Name', validators=[Required(), Length(max=100)])
    period = fields.SelectField(u'Period', validators=[Optional()], choices=get_period_choices())
    end_date = fields.DateField(u'End date', validators=[RequiredIfMissing('period')])

Also DateRange validator:

DateField(u'End date', validators=[DateRange(min=datetime.now, max=lambda: datetime.now() + timedelta(365), format='%d.%m.%Y')])

SelectField with optgroups

from wtforms import form, fields
from wtforms.validators import Required

def get_measure_choices():
    return [
        (u'Mass', [
            (u'kg', u'Kilogram'),
            (u't', u'Ton')]
        (u'Volume', [
            (u'l', u'Liter'),
            (u'm3', u'Cube')]

class ProductForm(form.Form):
    measure = fields.SelectField(u'Measure unit', validators=[Required()], choices=get_measure_choices())


Also now you can put html code to widgets without escaping:

from wtforms.widgets import HTMLString

SelectField(u'Group', validators=[Required()],
    choices=[(u'1', HTMLString(u'Level 1')),
             (u'2', HTMLString(u' Level 1.1')),
             (u'3', HTMLString(u' Level 1.2'))])