Matt Wilson schrieb:
> I got a controller that accepts a start_date and a stop_date
> parameter.  I want to validate that the stop_date is after the
> start_date.
> 
> I want to do this in my @validate, not in my controller guts.
> 
> I read the example schema validator about matching passwords, but I
> need more help.  Is this even possible?

Yes, you need to write a custom validator that can be used as a
chained_validator. This can then transform both given dates in to
datatime.date object and compare them. Have you read this?

http://docs.turbogears.org/1.0/FormValidationWithSchemas#using-custom-validators-in-schemas

The trick with chained validators is that they get passed a dictionary
of form values not a single value. Your validator needs to pick the two
dates from the dictionary and then do the validation on those. Another
cave-at is the error message. Instead of just raising
validators.Invalid, you need to set the error_dict as well, so that the
validation error messages only appear next to the fields with the errors
(and not all fields):

So the general structure for a validator that can be used as a chained
validator is:

class MyValidator(validators.FancyValidator):

    messages = dict(
        invalid = "Invalid value '%(value)s' for field '%(name)s'."
    )

    def validate_python(self, value, state):
        try:
            if isinstance(value, dict):
                myvalue = value.get('myvalue')
            else:
                myvalue = value
            # do whatever checks you need to do for validation here
            if not is_valid(myvalue):
                raise ValueEror
        except ValueError, exc:
            msg = self.message('invalid', state, name='myvalue',
                value=myvalue)
            if isinstance(value, dict):
                raise validators.Invalid(msg, value, state,
                    error_dict={'myvalue': msg})
            else:
                raise validators.Invalid(msg, myvalue, state)

HTH, Chris

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" 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/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to