In a form I am using a MultiValueField (MVF) with a MultiWidget that has several fields. If there is a validation error in one of the fields of the MVF, this gets handled (displayed) at the MVF level, rather than at the individual sub-fields, which can lead to:
* Ensure this value is greater than or equal to 1.
* Ensure this value is greater than or equal to -100.0.
Number of days: -1
...
...
Threshold: -200
Where the first error refers to the first field of the MVF and the second error the the last field of the MVF.
Is it possible to put those error messages "inside" the MVF at the field where they belong? (maybe in the format_output method of the MultiWidget?)
The following solution doesn't use a MultiValueField but instead:
dynamically replaces the original field with several ones on form's __init__
reconstruct valid data for the original field during the form validation on _post_clean
Here is some test code that needs to be adapted to each case:
class MyMultiField(CharField):
def split(self, form):
name = 'test'
form.fields_backup[name] = form.fields[name]
del form.fields[name]
# here is where you define your individual fields:
for i in range(3):
form.fields[name + '_' + str(i)] = CharField()
# you need to extract the initial data for these fields
form.initial[name + '_' + str(i)] = somefunction(form.initial[name])
form.fields['test_1'] = DecimalField() # because I only want numbers in the 2nd field
def restore(self, form):
# here is where you describe how to joins the individual fields:
value = ''.join([unicode(v) for k, v in form.cleaned_data.items() if 'test_' in k])
# extra step to validate the combined value against the original field:
try:
restored_data = form.cleaned_data.copy()
restored_data["test"] = form.fields_backup["test"].clean(value)
for k in form.cleaned_data:
if k.startswith("test_"):
del restored_data[k]
form.cleaned_data = restored_data
except Exception, e:
form._errors[NON_FIELD_ERRORS] = form.error_class(e)
class MyForm(Form):
test = MyMultiField()
def __init__(self, *args, **kwargs):
super(ItemForm, self).__init__(*args, **kwargs)
self.fields_backup = {}
self.fields['data'].split(self)
def _post_clean(self):
self.fields_backup['data'].restore(self)
return super(MyForm, self)._post_clean()
Before:
After (validating some input):
I'm not sure if it's possible to decouple this Field/Form code further using this approach. I'm also not quite satisfied with this code as the new field class needs to inherit from the original one.
Nonetheless, the basic idea is there and I successfully used it to individually validate form fields built from a dictionary stored in a single model field with PostgreSQL hstore.
Related
I am trying to add an error class to fields of a each form in a formset if a custom clean method detects errors. This does look to do the trick, I load the page, and the field does have the error class in it. but when in the template I add a custom filter to add a form-control class, everything falls apart.
# in my inlineformset:
def clean(self, *args, **kwargs):
if any(self.errors):
errors = self.errors
return
## 1) Total amount
total_amount = 0
for form in self.forms:
if self.can_delete and self._should_delete_form(form):
continue
amount = form.cleaned_data.get('amount')
total_amount += amount
if total_amount> 100:
for form in self.forms:
form.fields['amount'].widget.attrs.update({'class': 'error special'})
raise ValidationError(_('Total amount cannot exceed 100%'))
And, here is my code for the custom filter:
#register.filter(name = 'add_class')
def add_class(the_field, class_name):
''' Adds class_name to the string of space-separated CSS classes for this field'''
initial_class_names = the_field.css_classes() ## This returns empty string, but it should return 'error special'
class_names = initial_class_names + ' ' + class_name if initial_class_names else class_name
return the_field.as_widget(attrs = {'class': class_names,})
And, in my template:
{# {{ the_field|add_class:"form-control"}} #} #<- This adds the form-control, but removes the other classes added in the clean method
{{ the_field }} {# This shows the two classes for the offending fields, 'error special' #}
I think the problem is with the .css_classes() method which does not bring in the classes defined on the form. Remember, these classes have been set on these fields and rendering {{ the_field }} shows the classes were correctly passed down to the template. So, then the question is whether I am using the correct method .css_classes() or if I should use another method?
I was able to add class error to the field using .add_error method of the form. Although this works around the problem, I would still appreciate if anyone can explain how come the the_field.css_classes() returned an empty string instead of what was set in the clean method:
form.fields['amount'].widget.attrs.update({'class': 'error special'})
The problem with add_error method is that it only adds the class error. But, what if I'd want to add another class special to the widget? So, the original problem still needs an answer. My solution here then is just a solution and not the solution:
# in my inlineformset:
def clean(self, *args, **kwargs):
if any(self.errors):
errors = self.errors
return
## 1) Total amount
total_amount = 0
for form in self.forms:
if self.can_delete and self._should_delete_form(form):
continue
amount = form.cleaned_data.get('amount')
total_amount += amount
if total_amount > 100:
msg = 'This field throw the amount over the 100% limit'
form.add_error('amount', msg)
if total_amount> 100:
raise ValidationError(_('Total amount cannot exceed 100%'))
I have want to have an endpoint which is called by a third party which the parameter names they will use can't be changed. The submitted data contains parameters with the character . (e.g. hub.token) along with others.
I had wanted to create a Django form to validate the callback but how does one write a form class which will bind to the request. Without noticing the problem I tried hub.token which does not work as hub dictionary does not exist. I then tried hub_token in the hope Django would recognise the intent.
Having a period in the attribute is valid as in HTML form submission the name is taken the controls name attribute which can contain periods.
Is there an easy way to handle this situation without having to access each field in the view?
forms.py
from django import forms
class RegisterEndpointForm(forms.Form):
hub.token = forms.CharField()
views.py
...
register_request = RegisterEndpointForm(request.GET)
if register_request.is_valid()
pass
...
I don't thin you can set attributes containing dots. You could try a couple things. You could set the field as hub_token, then replace the dot with an underscore in request.GET. for example:
data = request.GET
data['hub_token'] = data['hub.token']
del data['hub.token']
register_request = RegisterEndpointForm(data)
…
You could also try setting the field dynamically on form init. For example:
class RegisterEndpointForm(forms.Form):
def __init__(self, *args, **kwargs):
super(RegisterEndpointForm, self).__init__(*args, **kwargs)
self.fields['hub.token'] = forms.CharField()
I have used the __init__ method suggested by #jproffitt but replace all keys containing periods with underscores.
class RegisterEndpointForm(forms.Form):
hub_verify_token = forms.CharField()
[...]
def __init__(self, *args, **kwargs):
super(RegisterEndpointForm, self).__init__(*args, **kwargs)
all_fields = self.data.copy()
for key, value in all_fields.iteritems():
safe_key = key.replace('.', '_')
if safe_key != key:
self.data[safe_key] = value
I'm using Django 1.4 with Python 2.7 on Ubuntu 12.10.
I have a form where I need to populate a few drop-downs dynamically (using jQuery) but need 2 of them to be required and the 3rd to be optional.
I'm using Tastypie to help with the API to get the options. Basically the first drop-down is populated with industry level codes for schools. Once a code is selected a category drop-down is populated for all categories for that code. Once the category is chosen a subcategory drop-down is populated for all subcategories for that combination of code and category.
I'm able to require the code drop-down (it's not dynamically populated). However, I'm having a tough time getting the category drop-down to be required. There are basically 2 routes I can take - front-end validation or back-end validation. I'm trying to go with back-end validation so I can easily create further validation if needed.
Here is the form:
class SchoolProductForm(forms.ModelForm):
cip_category = forms.ChoiceField(required=True,
choices=(('', '----------'),))
def __init__(self, *args, **kwargs):
super(SchoolProductForm, self).__init__(*args, **kwargs)
self.fields['short_description'].widget = TA_WIDGET
self.fields['salary_info'].widget = TA_WIDGET
self.fields['job_opportunities'].widget = TA_WIDGET
self.fields['related_careers'].widget = TA_WIDGET
self.fields['meta_keywords'].widget = TI_WIDGET
self.fields['meta_description'].widget = TI_WIDGET
self.fields['cip'].queryset = models.CIP.objects.filter(
parent_id__isnull=True)
class Meta:
model = models.SchoolProduct
exclude = ('campus',)
I've tried to override the clean method. I've tried to create a field specific clean method. Neither seem to work.
Variations of the following:
def clean(self):
super(SchoolProductForm, self).clean()
if cip_category in self._errors:
del self._errors['cip_category']
if self.cleaned_data['cip_category'] == '----------':
self._errors['cip_category'] = 'This field is required.'
return self.cleaned_data
This gives an error that there is no cip_category in cleaned_data, which makes sense because it didn't validate.
I've tried variations with the field specific clean:
def clean_cip_category(self):
data = self.cleaned_data['cip_category']
self.fields['cip_category'].choices = data
return data
But get a validation error on the page stating my choice is not one of the available choices.
I've tried to create a dynamic field type (several variations):
class DynamicChoiceField(forms.ChoiceField):
def valid_value(self, value):
return True
class SchoolProductForm(forms.ModelForm):
cip_category = DynamicChoiceField(required=True,
choices=(('', '----------'),))
But it accepts ---------- as a valid option (which I don't want) and causes an error since the ORM tries to match a value of ---------- in the database (which it won't find).
Any ideas?
I was able to solve this with a little overriding of a method in ChoiceField.
I added the field to the form and handled the pre-population with the self.initial:
class SchoolProductForm(forms.ModelForm):
cip_category = common_forms.DynamicChoiceField(
required=True, choices=(('', '----------'),))
def __init__(self, *args, **kwargs):
super(SchoolProductForm, self).__init__(*args, **kwargs)
self.fields['short_description'].widget = TA_WIDGET
self.fields['salary_info'].widget = TA_WIDGET
self.fields['job_opportunities'].widget = TA_WIDGET
self.fields['related_careers'].widget = TA_WIDGET
self.fields['meta_keywords'].widget = TI_WIDGET
self.fields['meta_description'].widget = TI_WIDGET
self.fields['cip'].queryset = models.CIP.objects.filter(
parent_id__isnull=True)
# Get the top parent and pre-populate
if 'cip' in self.initial:
self.initial['cip'] = models.CIP.objects.get(
pk=self.initial['cip']).top_parent()
class Meta:
model = models.SchoolProduct
exclude = ('campus',)
Where DynamicChoiceField is:
class DynamicChoiceField(forms.ChoiceField):
def valid_value(self, value):
return True
Then, in the view I added handling in the form_valid override:
def form_valid(self, form):
self.object = form.save(commit=False)
# Handle the CIP code
self.object.cip_id = self.request.POST.get('cip_subcategory')
if self.object.cip_id == '':
self.object.cip_id = self.request.POST.get('cip_category')
self.object.save()
I've just discovered that Django doesn't automatically strip out extra whitespace from form field inputs, and I think I understand the rationale ('frameworks shouldn't be altering user input').
I think I know how to remove the excess whitespace using python's re:
#data = re.sub('\A\s+|\s+\Z', '', data)
data = data.strip()
data = re.sub('\s+', ' ', data)
The question is where should I do this? Presumably this should happen in one of the form's clean stages, but which one? Ideally, I would like to clean all my fields of extra whitespace. If it should be done in the clean_field() method, that would mean I would have to have a lot of clean_field() methods that basically do the same thing, which seems like a lot of repetition.
If not the form's cleaning stages, then perhaps in the model that the form is based on?
My approach is borrowed from here. But instead of subclassing django.forms.Form, I use a mixin. That way I can use it with both Form and ModelForm. The method defined here overrides BaseForm's _clean_fields method.
class StripWhitespaceMixin(object):
def _clean_fields(self):
for name, field in self.fields.items():
# value_from_datadict() gets the data from the data dictionaries.
# Each widget type knows how to retrieve its own data, because some
# widgets split data over several HTML fields.
value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
try:
if isinstance(field, FileField):
initial = self.initial.get(name, field.initial)
value = field.clean(value, initial)
else:
if isinstance(value, basestring):
value = field.clean(value.strip())
else:
value = field.clean(value)
self.cleaned_data[name] = value
if hasattr(self, 'clean_%s' % name):
value = getattr(self, 'clean_%s' % name)()
self.cleaned_data[name] = value
except ValidationError as e:
self._errors[name] = self.error_class(e.messages)
if name in self.cleaned_data:
del self.cleaned_data[name]
To use, simply add the mixin to your form
class MyForm(StripeWhitespaceMixin, ModelForm):
...
Also, if you want to trim whitespace when saving models that do not have a form you can use the following mixin. Models without forms aren't validated by default. I use this when I create objects based off of json data returned from external rest api call.
class ValidateModelMixin(object):
def clean(self):
for field in self._meta.fields:
value = getattr(self, field.name)
if value:
# ducktyping attempt to strip whitespace
try:
setattr(self, field.name, value.strip())
except Exception:
pass
def save(self, *args, **kwargs):
self.full_clean()
super(ValidateModelMixin, self).save(*args, **kwargs)
Then in your models.py
class MyModel(ValidateModelMixin, Model):
....
Create a custom model field so that your custom form field will be used automatically.
class TrimmedCharFormField(forms.CharField):
def clean(self, value):
if value:
value = value.strip()
return super(TrimmedCharFormField, self).clean(value)
# (If you use South) add_introspection_rules([], ["^common\.fields\.TrimmedCharField"])
class TrimmedCharField(models.CharField):
__metaclass__ = models.SubfieldBase
def formfield(self, **kwargs):
return super(TrimmedCharField, self).formfield(form_class=TrimmedCharFormField, **kwargs)
Then in your models just replace django.db.models.CharField with TrimmedCharField
How about adding that to the def clean(self): in the form?
For further documentation see:
https://docs.djangoproject.com/en/dev/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other
Your method could look something like this:
def clean(self):
cleaned_data = self.cleaned_data
for k in self.cleaned_data:
data = re.sub('\A\s+', '', self.cleaned_data[k])
data = re.sub('\s+\Z', '', data)
data = re.sub('\s+', ' ', data)
cleaned_data[k]=data
return cleaned_data
Use the following mixin:
class StripWhitespaceMixin(object):
def full_clean(self):
# self.data can be dict (usually empty) or QueryDict here.
self.data = self.data.copy()
is_querydict = hasattr(self.data, 'setlist')
strip = lambda val: val.strip()
for k in list(self.data.keys()):
if is_querydict:
self.data.setlist(k, map(strip, self.data.getlist(k)))
else:
self.data[k] = strip(self.data[k])
super(StripWhitespaceMixin, self).full_clean()
Add this as a mixin to your form e.g.:
class MyForm(StripWhitespaceMixin, Form):
pass
This is similar to pymarco's answer, but doesn't involve copy-pasting and then modifying Django code (the contents of the _clean_fields method).
Instead, it overrides full_clean but calls the original full_clean method after making some adjustments to the input data. This makes it less dependent on implementation details of Django's Form class that might change (and in fact have changed since that answer).
Since Django 1.9 you can use the strip keyword argument in the field of your form definition :
strip¶
New in Django 1.9.
If True (default), the value will be stripped of leading and trailing whitespace.
Which should give something like :
class MyForm(forms.Form):
myfield = forms.CharField(min_length=42, strip=True)
And since its default value is True this should be automatic with django>=1.9.
It's also relevant with RegexField.
In this case, it could be useful to create your own form field (it's not that hard as it sounds). In the clean() method you would remove that extra whitespaces.
Quoting the documentation:
You can easily create custom Field classes. To do this, just create a
subclass of django.forms.Field. Its only requirements are that it
implement a clean() method and that its __init__() method accept the
core arguments (required, label, initial, widget,
help_text).
More about it: https://docs.djangoproject.com/en/1.3/ref/forms/fields/#creating-custom-fields
One way to do this is to specify custom form widget that strips whitespace:
>>> from django import forms
>>> class StripTextField(forms.CharField):
... def clean(self,value):
... return value.strip()
...
>>> f = StripTextField()
>>> f.clean(' hello ')
'hello'
Then to use this in your ModelForm:
class MyForm(ModelForm):
strip_field = StripTextField()
class Meta:
model = MyModel
However, the best place to do this is in your view after the form has been validated; before you do any inserts into the db or other manipulation of data if you are using ModelForms.
You can always create your own non-ModelForm forms and control every aspect of the field and validation that way.
ModelForm's validation adds checks for values that would violate the db constraints; so if the field can accept ' hello ' as a valid input, ModelForm's is_valid() would have no reason to strip the whitespaces (as it wouldn't make for arbitrary clean logic, in addition to what you mentioned "frameworks shouldn't alter user's input").
If you want to strip() every CharField in your project; it may be simplest to monkeypatch CharField's default cleaning method.
within: monkey_patch/__init__.py
from django.forms.fields import CharField
def new_clean(self, value):
""" Strip leading and trailing whitespace on all CharField's """
if value:
# We try/catch here, because other fields subclass CharField. So I'm not totally certain that value will always be stripable.
try:
value = value.strip()
except:
pass
return super(CharField, self).clean(value)
CharField.clean = new_clean
I would like to make an entire inline formset within an admin change form compulsory. So in my current scenario when I hit save on an Invoice form (in Admin) the inline Order form is blank. I'd like to stop people creating invoices with no orders associated.
Anyone know an easy way to do that?
Normal validation like (required=True) on the model field doesn't appear to work in this instance.
The best way to do this is to define a custom formset, with a clean method that validates that at least one invoice order exists.
class InvoiceOrderInlineFormset(forms.models.BaseInlineFormSet):
def clean(self):
# get forms that actually have valid data
count = 0
for form in self.forms:
try:
if form.cleaned_data:
count += 1
except AttributeError:
# annoyingly, if a subform is invalid Django explicity raises
# an AttributeError for cleaned_data
pass
if count < 1:
raise forms.ValidationError('You must have at least one order')
class InvoiceOrderInline(admin.StackedInline):
formset = InvoiceOrderInlineFormset
class InvoiceAdmin(admin.ModelAdmin):
inlines = [InvoiceOrderInline]
Daniel's answer is excellent and it worked for me on one project, but then I realized due to the way Django forms work, if you are using can_delete and check the delete box while saving, it's possible to validate w/o any orders (in this case).
I spent a while trying to figure out how to prevent that from happening. The first situation was easy - don't include the forms that are going to get deleted in the count. The second situation was trickier...if all the delete boxes are checked, then clean wasn't being called.
The code isn't exactly straightforward, unfortunately. The clean method is called from full_clean which is called when the error property is accessed. This property is not accessed when a subform is being deleted, so full_clean is never called. I'm no Django expert, so this might be a terrible way of doing it, but it seems to work.
Here's the modified class:
class InvoiceOrderInlineFormset(forms.models.BaseInlineFormSet):
def is_valid(self):
return super(InvoiceOrderInlineFormset, self).is_valid() and \
not any([bool(e) for e in self.errors])
def clean(self):
# get forms that actually have valid data
count = 0
for form in self.forms:
try:
if form.cleaned_data and not form.cleaned_data.get('DELETE', False):
count += 1
except AttributeError:
# annoyingly, if a subform is invalid Django explicity raises
# an AttributeError for cleaned_data
pass
if count < 1:
raise forms.ValidationError('You must have at least one order')
class MandatoryInlineFormSet(BaseInlineFormSet):
def is_valid(self):
return super(MandatoryInlineFormSet, self).is_valid() and \
not any([bool(e) for e in self.errors])
def clean(self):
# get forms that actually have valid data
count = 0
for form in self.forms:
try:
if form.cleaned_data and not form.cleaned_data.get('DELETE', False):
count += 1
except AttributeError:
# annoyingly, if a subform is invalid Django explicity raises
# an AttributeError for cleaned_data
pass
if count < 1:
raise forms.ValidationError('You must have at least one of these.')
class MandatoryTabularInline(admin.TabularInline):
formset = MandatoryInlineFormSet
class MandatoryStackedInline(admin.StackedInline):
formset = MandatoryInlineFormSet
class CommentInlineFormSet( MandatoryInlineFormSet ):
def clean_rating(self,form):
"""
rating must be 0..5 by .5 increments
"""
rating = float( form.cleaned_data['rating'] )
if rating < 0 or rating > 5:
raise ValidationError("rating must be between 0-5")
if ( rating / 0.5 ) != int( rating / 0.5 ):
raise ValidationError("rating must have .0 or .5 decimal")
def clean( self ):
super(CommentInlineFormSet, self).clean()
for form in self.forms:
self.clean_rating(form)
class CommentInline( MandatoryTabularInline ):
formset = CommentInlineFormSet
model = Comment
extra = 1
#Daniel Roseman solution is fine but i have some modification with some less code to do this same.
class RequiredFormSet(forms.models.BaseInlineFormSet):
def __init__(self, *args, **kwargs):
super(RequiredFormSet, self).__init__(*args, **kwargs)
self.forms[0].empty_permitted = False
class InvoiceOrderInline(admin.StackedInline):
model = InvoiceOrder
formset = RequiredFormSet
class InvoiceAdmin(admin.ModelAdmin):
inlines = [InvoiceOrderInline]
try this it also works :)
The situation became a little bit better but still needs some work around. Django provides validate_min and min_num attributes nowadays, and if min_num is taken from Inline during formset instantiation, validate_min can be only passed as init formset argument. So my solution looks something like this:
class MinValidatedInlineMixIn:
validate_min = True
def get_formset(self, *args, **kwargs):
return super().get_formset(validate_min=self.validate_min, *args, **kwargs)
class InvoiceOrderInline(MinValidatedInlineMixIn, admin.StackedInline):
model = InvoiceOrder
min_num = 1
validate_min = True
class InvoiceAdmin(admin.ModelAdmin):
inlines = [InvoiceOrderInline]