Posted By

wackysalut on 11/25/08


Tagged

python pylons formencode htmlfill


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

wackysalut


Full FormEncode + htmlfill example, with form and test values


 / 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.

  1. #!/usr/bin/python
  2.  
  3. import formencode
  4. from formencode import validators
  5. from formencode import htmlfill
  6.  
  7. class Valid(formencode.Schema):
  8. allo = validators.UnicodeString(not_empty=True)
  9. num = validators.Int()
  10. num2 = validators.Int(not_empty=True)
  11. pouet = validators.UnicodeString(not_empty=True)
  12. gna = validators.String(not_empty=True)
  13.  
  14.  
  15. a = """
  16. <form action="pouet" method="post">
  17. <input type="text" name="pouet" />
  18. <form:error name="pouet" />
  19.  
  20. <input type="password" name="allo" />
  21.  
  22. <input type="text" name="num" />
  23. <form:error name="num" />
  24.  
  25. <input type="text" name="num2" />
  26. <form:error name="num2" />
  27.  
  28. <textarea name="gna"></textarea>
  29. <form:iferror name="gna">Horrible horror message</form:iferror>
  30. <form:error name="gna" />
  31.  
  32. <textarea name="gna2"></textarea>
  33. <form:iferror name="gna2">Horrible horror message</form:iferror>
  34. <form:error name="gna2" />
  35.  
  36.  
  37. </form>
  38.  
  39. """
  40.  
  41. inp = {'allo': 'salut', 'pouet': 's', 'num': '123', 'num2': "123", 'gna': ''}
  42.  
  43.  
  44. ok = None
  45. try:
  46. ok = Valid.to_python(inp)
  47. except validators.Invalid, e:
  48. print e.value
  49. print htmlfill.render(a, {'gna2': 'salutataion'}, e.error_dict or {})
  50.  
  51.  
  52. print ok

Report this snippet  

You need to login to post a comment.