On May 4, 2007, at 10:53 PM, Malcolm Tredinnick wrote: > On Fri, 2007-05-04 at 15:00 -0700, Casey T. Deccio wrote: >> I'd like to be able to do some further validation in a form, based on >> the instance object from which the form was built (i.e., using >> form_from_instace). This would be handy for either populating fields >> (i.e., for an extended form class) using the instance or for >> validation >> after the form has been submitted, based on previous information. Is >> there a good way to do this? Would it be appropriate to modify >> form_for_instance to include an 'instance' attribute in the returned >> class (or something similar)? > > You could include the instance's primary key value in the form so that > you could retrieve it later. Although, remember that the person > submitting the form could change that value (even if it's a hidden > field), so you may wish to encrypt it in some fashion before including > it.
Well, in my case I'm submitting the object id as part of the URL (i.e., http://localhost/object/<id>/edit/). So when the form is built using form_from_instance, the form fields are created and pre- populated with the fields from the instance. However, I am using a child class of forms.Form (as a form argument to form_from_instance) to add additional fields to the form that is displayed (see my May 4 post on "newforms inheritance"), and I want one of the additional form fields to be populated based on one of the values of the current object--when the form is initially displayed (i.e., before POST). Having the object instance available for validation of POST data is also desirable (as you mentioned below). Maybe the instance could be passed as an class attribute in when form_for_instance returns type (...). To streamline validation, I've attempted to define full_clean in the child class of Form (which is the parent class of the dynamically- created ObjectInstanceForm). However, it is a bit tricky-- particularly with the private __errors variable. Regards, Casey > Part of your requirements -- validation based on existing > information -- > are something we want to implement as what we are calling "model-aware > validation", whereby models can do validation based on their current > contents and knowledge of "self". It isn't really implemented (or even > fully specified) yet, but it's something I want to look at before 1.0. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" 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/django-users?hl=en -~----------~----~----~----~------~----~------~--~---

