2

I have a form that asks for a FloatField, and IntegerField.

conc = forms.FloatField(label='Concentration (uM)',required=False)
muxlevel = forms.IntegerField(label='Multiplex level ',required=False)

If a user enters say 0.5, into the muxlevel field, form.is_valid() returns False, and the form is returned saying

"Enter a whole number."

Which is what I expect (it's awesome actually). But if user enters text into either field, say 'asdf', the validation doesn't complain, form.is_valid() returns True, and the value of the form.cleaned_data['muxlevel'] is None. The same thing happens for entering text into the FloatField conc.

That's not what I would have expected, I would rather the user get returned to the form saying "Enter a whole number" or "Enter a floating point value".

I've searched and don't see this problem reported. In fact the documentation says that...

  • Normalizes to: A Python float
  • Validates that the given value is a float
  • Leading and trailing whitespace is allowed, as in Python’s float() function.

EDIT - The view looks like

from .forms import *
def scr_query(request):
    summ = ''
    if request.method == "POST":
        form = QueryForm(request.POST)

        if form.is_valid():

            if (form.cleaned_data['conc']):         
                summ += "Concentration < " + str(form.cleaned_data['conc_upper']) + "<br>"
            if (form.cleaned_data['muxlevel']):

                summ += "Muxlevel > " + str(form.cleaned_data['muxlevel']) + "<br>"

and the form looks like this

class QueryForm(forms.Form):

    conc = forms.FloatField(label='Concentration (uM)',required=False)
    muxlevel = forms.IntegerField(label='Multiplex level ',required=False)

My reading tells me that python's float() actually gets called to check if the provided string can be coerced into a float. If I do that at the console using float('abc') I get the expected exception. So not sure why It's getting converted to None. Is there some form of text stripping happening before the call to float(), so that it's getting a blank value, and float('') is returning None, as expected?

Any assistance would be greatly appreciated.

Thanks.

6
  • Which version of Django are you using? Can you show your view? Commented Mar 29, 2016 at 16:11
  • @Alisdair, Django 1.8.9, post edited to show the view. Everything else works, it's just the validation that doesn't behave as expected. Thanks. Commented Mar 29, 2016 at 16:41
  • The indentation that you've posted doesn't look right. Can you show the full QueryForm? Try printing/logging request.POST to check that the values are present. Commented Mar 29, 2016 at 16:58
  • Code Fragment. Indents are fixed. The point is that the view prints out values when they're properly entered, trips a validation error when a non-integer number is entered for the IntegerField, but does not show a validation error if a string is entered into either of the form fields, instead returning a valid form, with a value of None for the variable. Commented Mar 29, 2016 at 19:19
  • If I do form = QueryForm({'conc': 'a string', 'muxlevel': 'another string'}); print(form.errors) I get the following errors <ul class="errorlist"><li>muxlevel<ul class="errorlist"><li>Enter a whole number.</li></ul></li><li>conc<ul class="errorlist"><li>Enter a number.</li></ul></li></ul>. Therefore I think that either there's something weird in your form that you haven't shown, or request.POST isn't what you think it is. Commented Mar 29, 2016 at 20:02

1 Answer 1

2

As @Alasdair has mentioned the problem is with the request.POST. It contains no data for field. But the reason for this is the widget. By default Django uses forms.NumberInput (depending on your localization settings ofc) and it prevents non-numerical data to be sent in request. So, if you change to muxlevel = forms.IntegerField(label='Multiplex level ',required=False, widget=forms.TextInput()) you will see correct error message.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.