IntegerField and the required validator is unintuitive? Always required?

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

On the other hand, it seems that it IntegerFieldmay only be required and that I should not use the required validator.

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

Just try to find some patterns here.

Update:

  • WTForms Version __version__ = '1.0.5'
  • Flask_WTF Version __version__ = '0.9.3'

The 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 a value.

enter image description here

Instead, I used a custom validator instead of a text field.

+4
1

wtforms Issue # 14 . "NullableIntegerField". . , 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

:

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}

. , . , , None.

+7

Source: https://habr.com/ru/post/1524445/


All Articles