Automatically trim newforms text fields

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
def make_trimming(field):
    def clean_hook(self, old_clean, value):
        result = old_clean(value)
        if isinstance(result, (str, unicode,)):
            result = result.strip()
        return result
    old_clean = field.clean
    import new
    field.clean = new.instancemethod(
        lambda self, value: clean_hook(self, old_clean, value),
        field, field.__class__)

More like this

  1. DefaultValueWidget by miracle2k 4 years, 6 months ago
  2. Getting dynamic model choices in newforms by ubernostrum 4 years, 11 months ago
  3. assign fields dynamically in newforms by rubic 4 years, 11 months ago
  4. Hidden Forms by insin 4 years, 7 months ago
  5. Form rendering using a template instead of builtin HTML by leoh 4 years, 8 months ago

Comments

(Forgotten your password?)