Parameters of web controllers that make up the application are what the developers of these applications want to validate. This is how external clients are able to get data into the application. Through the parameters of the controllers. It is thus important to ensure that this data is acceptable to the application.
With FormEncode, schemas may be defined by a controller. These schemas are defined on a per controller basis and include the parameters used by the controller. By using this method of parameter validation, each parameter of the controller can be validated at the same time instead of trying to validate each parameter individually.
However, with schemas that have many parameters to validate, more than one parameter may be at fault for the validation failure. In this case, the exception raised by the validation failure has no useful meaning as a whole. What the client needs is an explanation for why each individual field failed. An example of how this can be done is shown below.
#Example; Better formencode error handling.
import formencode
#A simple validation schema.
class Schema(formencode.Schema):
name=formencode.validators.String(not_empty=True)
#Get meaningful data from the formencode exception.
def format_error(e):
return e.error_dict
#Main.
if __name__=="__main__":
#Attempt to validate.
try:
Schema().to_python({"name":""})
except formencode.Invalid, e:
#Display the better error data.
print format_error(e)