python - Formencode and empty values -
how can write custom validator executed, when user has submitted empty or missing value?
i've tried overriding to_python, validate_python, _to_python, _validate_python (and more) methods none of these seem run if user has submitted empty or none value
i found able prevalidator i'm not sure if it's best way. problem doing way nothing else gets validated if captcha fails. can use chained_validators instead of pre_validators has inverse effect captcha validated if other fields pass.
class loginform(formencode.schema): ... captcha = formencode.validators.string(if_missing=none) pre_validators = [validators.captcha('captcha')] class captcha(formencode.validators.formvalidator): def __init__(self, captcha_name): self.captcha_name = captcha_name def validate_python(self, value_dict, state): if not captcha.test_captcha(value_dict.get(self.captcha_name)): raise formencode.invalid('captcha error', self.captcha_name, state, error_dict={self.captcha_name: 'captcha did not match'})
Comments
Post a Comment