Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntegerField and required validator unintuitive? Always required?

When I use a wtforms.IntegerField with required validator, I only ever get the message "this field is required".

On the other hand, it seems like an IntegerField can only be required and that I'm not supposed to use a required validator.

What's the logic here? Doesn't it make sense that this field just adds an is integer validator, and it should still be usable as a required field (but must be int) or not required field?

Just trying to spot some patterns here.

Update:

  • WTForms version is __version__ = '1.0.5'
  • Flask_WTF version is __version__ = '0.9.3'

Form

class OrderForm(Form):
    order_number = wtforms.IntegerField('Order Number', validators=[validators.Required()])

View

    def render_response(self):
        ctx = {}
        order_form = OrderForm()
        ctx['order_form'] = order_form

        if order_form.validate_on_submit(): pass
            # stuff.
        return render_template('support/warranty_form.html', **ctx)

Template

{% macro render_field(field) %}
    <div class="form-field-wrapper">
        <div class="form-field">
            {{ field(**kwargs) }}
        </div>
        {{ render_field_errors(field) }}
    </div>
{% endmacro %}

{{ render_field(order_form.order_number, placeholder="ORDER NUMBER") }}

If I remove the required validator, my form still requires the value.

enter image description here

I have just used a custom validator on a text field instead.

like image 875
Yuji 'Tomita' Tomita Avatar asked Jan 30 '14 08:01

Yuji 'Tomita' Tomita


1 Answers

wtforms Issue #14 might interest you. The user was hoping to try to create a "NullableIntegerField". One idea that came was to use the optional validator. You can see how this works in the below script:

from werkzeug import MultiDict
from wtforms import Form, IntegerField, validators

class MyDefaultForm(Form):
    my_field = IntegerField('My Field')

class MyRequiredForm(Form):
    my_field = IntegerField('My Field', [validators.required()])

class MyOptionalForm(Form):
    my_field = IntegerField('My Field', [validators.optional()])

input_data = MultiDict({
    'my_field' : ''
})

default_form = MyDefaultForm(input_data)
required_form = MyRequiredForm(input_data)
optional_form = MyOptionalForm(input_data)

print 'Default Form Validation: {0}'.format(default_form.validate())
print 'Default Form Errors: {0}'.format(default_form.errors)
print 'Default Data: {0}'.format(default_form.data)
print

print 'Required Form Validation: {0}'.format(required_form.validate())
print 'Required Form Errors: {0}'.format(required_form.errors)
print 'Required Data: {0}'.format(required_form.data)
print

print 'Optional Form Validation: {0}'.format(optional_form.validate())
print 'Optional Form Errors: {0}'.format(optional_form.errors)
print 'Optional Data: {0}'.format(optional_form.data)
print

The results:

Default Form Validation: False
Default Form Errors: {'my_field': [u'Not a valid integer value']}
Default Data: {'my_field': None}

Required Form Validation: False
Required Form Errors: {'my_field': [u'This field is required.']}
Required Data: {'my_field': None}

Optional Form Validation: True
Optional Form Errors: {}
Optional Data: {'my_field': None}

I create three forms with an integer field. One with no validators, one with the required validator, and one with optional validator. You probably will want to use the optional validator so that the result is None.

like image 107
Mark Hildreth Avatar answered Sep 28 '22 02:09

Mark Hildreth