Re: formEncode - complicate validate form
Ian Bicking <[email protected]>
| Newsgroups | gmane.comp.python.formencode |
|---|---|
| Message-ID | <[email protected]> |
Grzesiek Slusarek wrote:
> Ian Bicking wrote:
> "
> You want a FormValidator, because you are validating pieces
> together.
> An example of this is
> formencode.validators.RequireIfMissing/RequireIfPresent. You
> might want
> to look at the implementation; how you use that field is:
>
> class MySchema(Schema):
> other fields...
> chained_validators = [RequireIfPresent(required='birthday',
> present='i_am_over_18')]
>
> Form validators validate the entire dictionary that is submitted.
> "
>
> As I understand in this example both birthday and i_am_over_18
> are form field. But what if I want to do validate some field of
> form ONLY if other field has a specific value?
> i saw source of formencode.validators.RequireIfMissing/
> RequireIfPresent and value of field can be achieved by e.g. value
> _dict.get(self.missing) but how called such this validator.
> This part i don't understand. And is it possible?
A FormValidator validates the entire form at once. So, maybe:
class RequireIfEqual(FancyValidator):
# Field name:
field = None
# expected value:
value = None
other_field = None
other_validator = None
def _to_python(self, form, state):
if form.get(self.field) == self.value:
conv = self.other_validator.to_python(form.get(self.other),
state)
form = form.copy()
form[self.other_field] = conv
return form
I think that's roughly what you are describing.