Re: Can a schema validator make new variables out of other variables?
W-Mark Kubacki <wmark-/[email protected]> Sat, 16 Aug 2008 23:23:44 +0200
| Newsgroups | gmane.comp.python.formencode |
|---|---|
| Message-ID | <2C8837732ABE5746BADC1321F498A3F52BB64579F5@chlorium> |
>From: formencode-discuss-bounces-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
>[mailto:formencode-discuss-bounces-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org] On Behalf Of
>Matthew Wilson
>I have a date field in my form and a time field in my form.
>
>I want to store a datetime in my database, so I'm manually combining
>these two fields in my app code. Is there any way to do this
>automatically with form code?
You could write your own validator to be used in "chained_validators":
class FieldJoiner(FancyValidator):
"""
Joins fields into one by using the specified delimiter.
::
>>> fj = PostalCodeInCountryFormat('fdatetime', ' ', ('fdate', 'ftime'))
>>> fj.to_python({'fdate': '2008-08-08', 'ftime': '08:08:08'})
{'fdatetime': '2008-08-08 08:08:08', 'fdate': '2008-08-08', 'ftime': '08:08:08'}
>>> fj.to_python({'country': 'DE', 'zip': '30029'})
{'country': 'DE', 'zip': '30029'}
"""
result_fieldname = 'fdatetime'
delimiter = ', '
fields_to_be_joined = 'zip'
__unpackargs__ = ('result_fieldname', 'delimiter', 'fields_to_be_joined')
def validate_python(self, fields_dict, state):
jl = [fields_dict[key] for key in self.fields_to_be_joined \
if key in fields_dict]
fields_dict[self.result_fieldname] = self.delimiter.join(jl)
... looks handy. Does it do the job?
-- Mark
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/