20

I know is_valid() on a bounded form checks if all required data is entered. This is not what I want. I want to check if any field was filled on the form.

Any ideas?

Elaborating:

I want to give the user the choice of not filling in the form at all. However, if they attempt to fill it in (ie: changed a value in a field from it's initial value---usually empty value), I want to validate it.

The view would be something like this:

def get_opinion(request):

    if request.method == 'POST':
        f = OpinionForm(request.POST)
        if form_is_blank(f):
            return HttpResponseRedirect(reverse('thank_you_anyway'))
        elif f.is_valid():
            #Process here
            return HttpResponseRedirect(reverse('thanks_for_participating'))
    else:
        initial = {'weekday': date.today().strftime('%A')}
        f = OpinionForm(initial=initial)

    return render_to_response(
        'get_opinion.html',
        {'form': f,},
        RequestContext(request)
    )

What I want is the form_is_blank() part.

2
  • You should put more of a distinct use case for this. If you just care about having at least one field filled in then that sounds like something that should happen client side with some javascript. Jan 1, 2011 at 1:06
  • @Matt: Thanks for the suggestion. I elaborated with some code. Jan 1, 2011 at 16:53

5 Answers 5

30

Guess I have to answer my own question.

Apparently, there's an undocumented Form function: has_changed()

>>> f = MyForm({})
>>> f.has_changed()
False
>>> f = MyForm({'name': 'test'})
>>> f.has_changed()
True
>>> f = MyForm({'name': 'test'}, initial={'name': 'test'})
>>> f.has_changed()
False

So this would do nicely as the replacement for form_is_blank() (reverted of course).

2
  • What if you want to save the form if it isn't blank? The has_changed method wouldn't help there, would it?
    – user9903
    Oct 11, 2012 at 16:26
  • Why do you duplicate the f = MyForm({}) case?
    – x-yuri
    Mar 26, 2020 at 19:58
4

To get this functionality work for subset of Forms used in the actual <form> tag you also need to define

class YourForm(forms.ModelForm):
    def full_clean(self):
        if not self.has_changed():
            self._errors = ErrorDict()
            return

        return super(YourForm, self).full_clean()

so when the user is prompted to fix validation errors it doesnt display errors from the forms which you want to validate only if some value isn't blank (or default).

3

You should set required=False for all fields in the form and override the clean method to do custom validation. See the current documentation for more.

1
  • For my particular use case, I have multiple forms in the view. If the user starts filling out one of the forms, then I want certain fields to be required.
    – Bobort
    Apr 10, 2017 at 16:06
1

If you have put required=True in your forms field or in the model blank=False so is_valid() should return False.

0

Use modelformset_Factory and pass your custom formset to it

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.