On 10/7/05, Jason Huggins <[EMAIL PROTECTED]> wrote:
> By default, it looks like validators in Django are only given the new
> field's value and the posted form data... I have a custom validator for
> my model that depends on information only available in the request. How
> can I get to the request object from inside the validator?

Correct: validators don't have access to request objects. Validators
are decoupled from the concept of HTTP requests. Adding a request
parameter to validators is *not* a good idea...

In your case, I'd explicitly pass the request-object's data to the
manipulator. Here's an example, playing off of the example from
http://www.djangoproject.com/documentation/forms/ :

    manipulator = ContactFormManipulator()
    if request.POST:
        new_data = request.POST.copy()

        # Manually pass IP address to new_data.
        new_data['ip_address'] = request.META['REMOTE_ADDR']

        errors = manipulator.get_validation_errors(new_data)
        if not errors:
            manipulator.do_html2python(new_data)

            # Send e-mail using new_data here...

            return HttpResponseRedirect("/contact/thankyou/")
    else:
        errors = new_data = {}

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org

Reply via email to