The code was placed inside a helper file without using a class. The Django validator was not designed to work with validator classes, it would appear, so retrieving the value from the field proved to be a hassle. Just create a helper file, import it on your model, and use the validator in the standard way, as such:
cnpj = models.CharField(unique=True, max_length=14, validators=[validate_CNPJ])
cpf = models.CharField(unique=True, max_length=14, validators=[validate_CPF])
A validator to check that an uploaded file has one of the given extensions.
It **does not** check the MIME type/ any file contents.
from django import forms
from .validators import FileTypeValidator
class UploadForm(forms.Form):
csv_file = forms.FileField(label="Upload file",
validators=[FileTypeValidator(('csv', 'txt'))])
This validator works well with FileField form fields and can validate that an uploaded file has an acceptable mimetype. Place this snippet in your app's `validators.py`.
Requirements:
This snippet uses [python-magic](https://github.com/ahupp/python-magic). To install:
pip install python-magic
Usage (in forms.py):
from validators import MimetypeValidator
class MyForm(forms.Form):
file = forms.FileField(
allow_empty_file=False,
validators=[MimetypeValidator('application/pdf')],
help_text="Upload a PDF file"
)
Validate form field that include email or emails separated by 'token' kwargs, by default ',' a comma. Return a list [] of email(s). Check validity of the email(s) from django EmailField regex (tested with 1.3, but normally will also work with 1.5)
If you inherit from ValidatedModel instead of from models.Model, then full_clean() will be called before save().
So, add validators to your field definitions, and all your fields will be validated before they go to the database.
The same thing can be accomplished with a pre_save signal, but the code is quite a bit messier than the simple inheritance above.
Out of the box, Django e-mail fields for both database models and forms only accept plain e-mail addresses. For example, `[email protected]` is accepted.
On the other hand, full e-mail addresses which include a human-readable name, for example the following address fails validation in Django:
Joe Hacker <[email protected]>
This package adds support for validating full e-mail addresses.
**Database model example**
from django import models
from full_email.models import FullEmailField
class MyModel(models.Model):
email = FullEmailField()
**Forms example**
from django import forms
from full_email.formfields import FullEmailField
class MyForm(forms.Form):
email = FullEmailField(label='E-mail address')
I maintain this code in a [GitHub gist](https://gist.github.com/1505228). It includes some unit tests as well.
Simple password validation for user registration - requires that password be 7 or more characters and contain both letters and numbers. Original validation with regex approach developed by kurtis. Optimized no-regex version based on code from watchedman ran as fast or significantly faster on all systems on which we tested it.
This django field can be used to introduce Spanish national Identification Numbers (NIF). It validates the NIF format and checks if the last letter is correct.
It also validates NIE numbers for foreign people.
You can use the field in your own forms:
'code'
dni = DNIField(max_length=15, widget=forms.TextInput(), label=_('DNI'))
'code'
I needed to overwrite files on update (not create new ones) but also a validation which would prevent to upload 2 files with the same name.
The CustomCheckFiled triggers a validation passing the filename and model instance. If the validation returns false, the validation error_message will be displayed in admin.
The OverwriteStorage is needed because the default storage alters the name if such name already exists. Enjoy.
I basically mixed both BRCPFField and BRCNPJField to create a widget that validates either an CPF or an CNPJ. The doc strings are not localized. So you probably have to hardcode it yourself.
Usage described in this blog post: [Django: FileField with ContentType and File Size Validation](http://nemesisdesign.net/blog/coding/django-filefield-content-type-size-validation/)
Snippet inspired by: [Validate by file content type and size](http://djangosnippets.org/snippets/1303/)
For disabling autocomplete and security purpose, this snippet defines a CharField with a randomness name for each request of the form.
This is useful for turning off autocomplete for credit card input in all browsers, without breaking the xhtml validation.
* [https://wiki.mozilla.org/The_autocomplete_attribute_and_web_documents_using_XHTML#Security](https://wiki.mozilla.org/The_autocomplete_attribute_and_web_documents_using_XHTML#Security)
* [http://en.wikipedia.org/wiki/Cryptographic_nonce](http://en.wikipedia.org/wiki/Cryptographic_nonce)