Archive for the tag 'formencode'

FieldJoiner Validator for DateTime

I’ve noticed on the FormEncode’s mailing lists Matthew Wilson asked about an validator joining two fields into one, so he can pass input of date and time directly to his database. Indeed, why did such an validator not exist? For your convenience, I’ve written it. Use it in chained_validators section of your validation schema:

class FieldJoiner(FancyValidator):
    """
    Joins fields into one by using the specified delimiter.

    ::

        >>> fj = FieldJoiner('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)

new validators in FormEncode

Since yesterday you can find another set of country, language and address validators in FormEncode. The most convenient and underestimated is InternationalPhoneNumber, which converts phone numbers in standardized format understood in many countries. Plus, by having data stored in the same format no matter where it has been entered, you can easily convert it to the local format – in the US, just replace “+1-” by empty string. Voila.

An easy way of letting your user enter phone numbers as usual and to add his/her country code stored with his account is:

from formencode.national import InternationalPhoneNumber

def PhoneNumber(*args, **kw):
    def get_cc():
        try:
            return identity.current.user.phone_cc
        except:
            return 49
    return InternationalPhoneNumber(default_cc=get_cc, *args, **kw)

PagingDataGrid and Formencode’s addressing

Fortunately I had the time to mortice some documentation about my PagingDataGrid for Prototype. The ExtJS version is opulent (over 300kb) in comparison to my less-than 22kb version. I wish I had found that grid ready when having started development with TurboGears…

Let’s see how the feedback on TG’s mailing lists is. AJAJForms seem to have been ignored, although everyone whom I showed it at University of Hanover was excited about it.

And, finally the country/addressing validators have found its way into Ian’s FormEncode. If I find the time by the end of the week I will talk with him about moving language validators into FE as well along with some other goodies, such as validators asking webservices to complete address informations (streets, county names, coordinates…).