Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access other field values from a validator in a Schema (not chained_validators) #119

Open
NucleaPeon opened this issue Sep 16, 2016 · 0 comments

Comments

@NucleaPeon
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant