Re: Renaming fields using a Schema
Ian Bicking <[email protected]>
| Newsgroups | gmane.comp.python.formencode,gmane.comp.web.turbogears |
|---|---|
| Message-ID | <[email protected]> |
Ed Singleton wrote:
> I'm using a FormEncode Schema to validate a dictionary, and I was
> wondering if it would be easy to get the validator to rename fields on
> the way through?
>
> Something like:
>
> data = {'email': '[email protected]'}
>
> class MyValidator(validators.Schema):
> email = validators.Email(not_empty=True, new_field_name='user_email')
>
> my_validator = MyValidator()
>
> valid_data = my_validator.to_python(data)
>
> print valid_data
>
> ## {'user_email': '[email protected]'}
You'll need to use a form validator, like:
class RenameField(FormValidator):
def __init__(self, start_name, end_name, **kw):
FormValidator.__init__(
self, start_name=start_name, end_name=end_name, **kw)
def _to_python(self, value, state):
value[end_name] = value[start_name]
def _from_python(self, value, state):
value[start_name] = value[end_name]
class MySchema(Schema):
pre_validators = [RenameField('email', 'user_email')]
--
Ian Bicking | [email protected] | http://blog.ianbicking.org
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV