I made a FormValidator subclass called DatefieldsValidator (below) ,
which validates a day/month/year field

how should I push errors from there into the template ?  can helpers
be used?


---
class DatefieldsValidator(formencode.validators.FormValidator):
    """
    Checks if a date is valid

    DatefieldsValidator('birth_year','birth_month','birth_day',
require_years_ago=13)
    """
    # tells UniqueNameValidator where to unpack the args passed to
__init__
    __unpackargs__ = ('*','field_names')
    field_names = None

    require_years_ago= None

    messages = {
        'date_invalid':"The date you submitted (%(date_string)s) is
not a real date.",
        'date_too_late':"The date you submitted (%(date_string)s) is
not before the cutoff date (%(latest_date_string)s).",
    }

    def validate_python(self, field_dict, state):
        print self.__dict__
        date_string= "%04d-%02d-%02d" % (
           int(field_dict[self.field_names[0]]),
           int(field_dict[self.field_names[1]]),
           int(field_dict[self.field_names[2]]),
        )
        date= None
        try:
            date= time.strptime(date_string,"%Y-%m-%d")
        except:
            msg = self.message('date_invalid', state, date_string=
date_string)
            raise formencode.api.Invalid(msg, field_dict, state,
error_dict={'form':msg})

        if self.require_years_ago:
            latest_date_string= datetime.date.today() -
relativedelta(years= self.require_years_ago)
            latest_date= time.strptime( "%s"%latest_date_string, "%Y-
%m-%d")
            timedelta= time.mktime(latest_date) - time.mktime(date)
            if timedelta < 0:
                msg = self.message('date_too_late', state,
date_string= date_string, latest_date_string= latest_date_string)
                raise formencode.api.Invalid(msg, field_dict, state,
error_dict={'form':msg})





--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to