/ Published in: Python
This simple example tests and shows off a fully working FormEncode (v1.2) example. I've had a hard time finding an example, so I now give it to you.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#!/usr/bin/python import formencode from formencode import validators from formencode import htmlfill class Valid(formencode.Schema): allo = validators.UnicodeString(not_empty=True) num = validators.Int() num2 = validators.Int(not_empty=True) pouet = validators.UnicodeString(not_empty=True) gna = validators.String(not_empty=True) a = """ <form action="pouet" method="post"> <input type="text" name="pouet" /> <form:error name="pouet" /> <input type="password" name="allo" /> <input type="text" name="num" /> <form:error name="num" /> <input type="text" name="num2" /> <form:error name="num2" /> <textarea name="gna"></textarea> <form:iferror name="gna">Horrible horror message</form:iferror> <form:error name="gna" /> <textarea name="gna2"></textarea> <form:iferror name="gna2">Horrible horror message</form:iferror> <form:error name="gna2" /> </form> """ inp = {'allo': 'salut', 'pouet': 's', 'num': '123', 'num2': "123", 'gna': ''} ok = None try: ok = Valid.to_python(inp) except validators.Invalid, e: print e.value print htmlfill.render(a, {'gna2': 'salutataion'}, e.error_dict or {}) print ok