Hello, I have form class that has a relatively lengthy clean method which does field validation (as the validity of fields depends on the values of fields) as well setting error messages on the form itself. I also intend to be part of an app used in multiple views in a few projects.
I want to run certain actions (aside from the usual slew of validation failure messages) based upon 1) the view in the which the form is being used 2) which clean checks fail. These actions may also need to alter the http response, possibly adding items to the context of the template to be rendered and possibly changing which template to use all together. I'm relatively new to the Django framework and I was wandering if anyone had ideas what might a good way to go about this. Below are current thoughts on the problem: + Have the validation function set variables on the form when validation checks fail and use these in the view to determine what action to run. The problem with this is would mean subclassing existing Django classes and altering them to set attributes when the built in validation checks fail. + To have signals fired in the form clean method based upon which validation checks fail. This suffers from the same problem as the last approach (reimplementing Django internals) and as the actions to be taken may need to alter the http response the http response would probably have to be passed to the form and then to the signals. This may be a better approach than the last one, but it feels awkward having to kick a response object around and it doesn't seem right having the output built beyond the views control. + Subclassing the form, breaking up the clean method in the parent form and then stringing these clean methods together in the child child method while splicing the actions in. The downsides to this would be: all the child forms would have to be altered if the parent form changed (and worse the form could end up in an app used my multiple projects, which would mean changing child forms in a quite few places); chopping up the validation check isn't easy job and running all the checks actions and splicing in the actions in the child clean method would be fairly lengthy; it feels like the actions should not be the form's responsibility. + Using the error messages set on the form to determine what validation errors have occurred. I can't see how this approach would work as I would consider the error messages malleable, and worse localised, making it hard to test against them. The only way I could see this working is if the messages where replaced with canonical messages and then use these to build humanized messages for display, but this seems messy. Cheers Aubrey -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.