Again, I'm not sure, but I think to do it at Model validation level you'd have to modify some of the django core files themselve. I think what you're looking for is in django.core.validators (in my install this file is at /lib/python2.7/site-packages/django/core/validators.py, or you can just search for validators.py on your system) But I've had my own troubles that I thought I could solve by modifying the django files themselves (and it works), but everyone that helped me advised that this is not the best way to do it, because future updates of django itself will break your changes.
I think the best way to do it is in your view that handles the form. For point 2., just make you MultipleEmailField() a CharField instead, and validate it in your view. For example, you could do something like this (note that I'm pulling lots of this from the docs and here http://stackoverflow.com/questions/3217682/checking-validity-of-email-in-django-python, and I haven't tested it) from django.shortcuts import render from django.http import HttpResponseRedirect def your_form_handling_view(request): if request.method == 'POST': # If the form has been submitted... form = YourForm(request.POST) # A form that you created in your forms.py if form.is_valid(): # All validation rules pass # Process the data in form.cleaned_data...get all the fields that require further validation your_multipleemail_field = form.cleaned_data['your_multipleemail_field'] #.... perform the extra validation here, for example extra_validation_passes = True for email in your_multipleemail_field.split(','): if email_id_exists(email) and not(validateEmail(email)): #assuming some_field is the email id and email_id_exists() is your function that validates it #Create your custom errors here form.your_multipleemail_field.errors = "Email id already exists in LDAP or is invalid" extra_validation_passes = False if extra_validation_passes: return HttpResponseRedirect('/thanks/') # Redirect after POST return render(request, 'template_with_your_form_on_it.html', {'form': form,}) #Now this form goes back to the template with your custom errors. else: form = YourForm() # An unbound form return render(request, 'template_with_your_form_on_it.html', {'form': form,}) This is a separate global function you might want to put somewhere other than views.py (maybe models.py, but don't forget to import it in views.py if you do this) def validateEmail( email ): from django.core.validators import validate_email from django.core.exceptions import ValidationError try: validate_email( email ) return True except ValidationError: return False Again, not sure if this works, I've never added custom errors to a form like this, but I think forms are normal objects and if the .errors are just strings, I don't see why it wouldn't work. In fact it probably doesn't work but it might give you some ideas. Of course, you could split the conditional "if email_id_exists(email) and not(validateEmail(email)):" to test them separately to give more precise error messages. Hope that helps. On Friday, November 2, 2012 4:36:20 AM UTC-4, Dilip M wrote: > Hi, > > I am new to Django. Went through docs before posting this.. I have a model > and form like this. > > models.py: > > class Recipients(models.Model): > dev = models.EmailField() > qa = models.EmailField() > cc = models.MultipleEmailField() > > forms.py: > > class RecipientsForm(forms.ModelForm): > > class Meta: > model = Recipients > > > Now I want to, > > 1. Add *additional* validation for models.EmailField(). Something like > check if email id entered exists in LDAP db. > 2. Create new model custom field MultipleEmailField(), which would split > emails separated by comma and uses modified validation of > models.EmailField() done in step 1. > > I am going through docs, but not able to figure out how to put things > together! Here is what I understood. > > MultipleEmailField() should go in <project-name>/fields.py. But how to > make it to run default validation of models.EmailField() and than do custom > validation? > > > Any help appreciated.. > > > Thanks. Dilip > -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/O7a9stFORSUJ. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.