Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask WTForms: Difference between DataRequired and InputRequired

What is difference between DataRequired and InputRequired in wtforms.valiadators

I have some fields in my signup form :

username password  password_repeat  submit 

Should these fields use the DataRequired or InputRequired validator?

like image 600
Ryan Avatar asked Jun 01 '14 18:06

Ryan


People also ask

What are validators class of WTForms in flask?

A validator simply takes an input, verifies it fulfills some criterion, such as a maximum length for a string and returns. Or, if the validation fails, raises a ValidationError . This system is very simple and flexible, and allows you to chain any number of validators on fields.

What is WTForms flask?

Flask WTForms is a library that makes form handling easy and structured. It also ensures the effective handling of form rendering, validation, and security. To build forms with this approach, you start by creating a new file in our app directory and name it forms.py. This file will contain all the application forms.

Should I use WTForms?

WTForms are really useful it does a lot of heavy lifting for you when it comes to data validation on top of the CSRF protection . Another useful thing is the use combined with Jinja2 where you need to write less code to render the form. Note: Jinja2 is one of the most used template engines for Python.

What is WTForms used for?

WTForms is a Python library that provides flexible web form rendering. You can use it to render text fields, text areas, password fields, radio buttons, and others. WTForms also provides powerful data validation using different validators, which validate that the data the user submits meets certain criteria you define.


1 Answers

Short Answer

Unless you have a good reason you should use InputRequired

Why?

Lets look at some notes from the docs/code for DataRequired() :

Note there is a distinction between this and DataRequired in that InputRequired looks that form-input data was provided, and DataRequired looks at the post-coercion data.

and

NOTE this validator used to be called Required but the way it behaved (requiring coerced data, not input data) meant it functioned in a way which was not symmetric to the Optional validator and furthermore caused confusion with certain fields which coerced data to 'falsey' values like 0, Decimal(0), time(0) etc. Unless a very specific reason exists, we recommend using the :class: InputRequired instead.

what does this mean?

In the Form class you will notice two keyword arguments formdata and data. These generally correspond with two methods process and process_formdata. When form data comes in off the wire its not always in the format corresponding to the Field type. A good example of this is the value u'1' being supplied to an IntegerField. This would be bad news if you had a NumberRange validator because u'1' isn't a number.

The primary purpose of the process_formdata method is to prevent this situation by coercing the value into its correct type prior to running validation rules. That is what they are referring to when they say "looks at the post-coercion data"

the problem!

Both InputRequired and DataRequired work the same way specifically the __call__ implementations:

def __call__(self, form, field):     if not field.data or isinstance(field.data, string_types) and not field.data.strip():         if self.message is None:             message = field.gettext('This field is required.')         else:             message = self.message 

Certain field types coerce data into Falsey values(0, Decimal(0), etc.). The problem occurs when you have an IntegerField and the form submits a value like '0'. If you apply DataRequired to this it will fail validation. This is because DataRequired will evaluate if not field.data... after coercion where field.data is the Falsey numeric value 0.

like image 195
nsfyn55 Avatar answered Oct 14 '22 21:10

nsfyn55