+
+class ProfileEditForm(Form):
+ name = fields.TextField(u'Display name', [validators.Required()])
+
+ def validate_name(form, field):
+ if not 1 < len(field.data) <= 20:
+ raise ValidationError("Name can't be longer than 20 characters")
+
+ any_real_characters = False
+ for char in field.data:
+ cat = unicodedata.category(char)
+
+ # Non-spacing marks and spaces don't count as letters
+ if cat not in ('Mn', 'Zs'):
+ any_real_characters = True
+
+ # Disallow control characters, format characters, non-assigned,
+ # private use, surrogates, spacing-combining marks (for Arabic,
+ # etc), enclosing marks (millions sign), line-spacing,
+ # paragraph-spacing.
+ # This also, thankfully, includes the RTL characters.
+ if cat in ('Cc', 'Cf', 'Cn', 'Co', 'Cs', 'Mc', 'Me', 'Zl', 'Zp'):
+ raise ValidationError("Please don't play stupid Unicode tricks")
+