-
Notifications
You must be signed in to change notification settings - Fork 72
Description
I'll use your PhoneInput example (from the RequiredIfMissing docstring) to explain:
I have a Schema with a phone number. I want to have an international phone number field that adds an international calling code to the existing phone number. You specify the field name and it adds the country code + phone number to itself.
The international phone number should be accessible though the schema's to_python() method
import formencode
import formencode.validators as validators
class InternationalPhoneNumber(validators.FancyValidator):
phone = None
def __init__(self, fieldname, **kwargs):
super(InternationalPhoneNumber, self).__init__()
self.phone = fieldname
def _to_python(self, value, instance):
# somehow find the schema's ``phone`` field and return it here:
# phonenumber = get_field_value_from_schema(self.phone)
return "1-" + value
class PhoneInput(formencode.Schema):
phone = validators.PhoneNumber()
phone_type = validators.String(if_missing=None)
int_phone = InternationalPhoneNumber("phone", if_missing=None)
PhoneInput().to_python({"phone": "111-111-1111", "int_phone": "phone"})
This code will output: {'phone_type': None, 'phone': '111-111-1111', 'int_phone': '1-phone'} but I would want it to output: {'phone_type': None, 'phone': '111-111-1111', 'int_phone': '1-111-111-1111'}
How would I go about making that happen?
Using chained_validators is sort of what I'm looking for, but all it does is validate, not add the resulting validator value back into the schema.
Any help is appreciated