Comments (8)
@Daverball I can't send the raw data. Because I use the API to submit form data from my website and also to use the API externally (outside the browser).
from wtforms.
Hi. Can you describe your usecase?
Currently choices
is not expected to be a dynamical list, but it can be a method that would be called when the Form
object is created. Maybe you can do what you want with select = wtforms.SelectField(choices=lambda: ["foo", "bar"])
?
from wtforms.
Hello,
See my code example:
from wtforms import Form, SelectField, validators
class MyForm(Form):
def render_visitor_timer_choices():
choices = [
(1, 'One'),
(2, 'Two'),
(3, 'Three'),
(4, 'Four')
]
return choices
visitor_timer = SelectField(
label='Time in seconds',
validators=[
validators.Optional(),
validators.NumberRange(
min=1,
max=4
)
],
choices=render_visitor_timer_choices,
coerce=int,
validate_choice=True,
render_kw={
'data-required': 'true'
}
)
form = MyForm(data={'visitor_timer': 5})
print(form.visitor_timer.choices) # Result: [(1, 'One'), (2, 'Two'), (3, 'Three'), (4, 'Four')]
print(form.validate()) # Result: True
print(form.visitor_timer.data) # Result: 5
"""
The response from *form.validate()* is True for the value 5 which is not in the SelectField's choices.
Shouldn't *form.validate()* be False?
"""
from wtforms.
Sharing my investigation: the minimal form I could write to reproduce this is:
>>> from wtforms import Form, SelectField, validators
... from werkzeug.datastructures import ImmutableMultiDictMixin
...
... class MyForm(Form):
... visitor_timer = SelectField(
... validators=[validators.Optional()],
... choices=[('1', 'One')],
... )
...
... form = MyForm(data={'visitor_timer': '5'})
... print(form.validate())
True
The validation result is False
as expected if the Optional
validator is removed:
>>> from wtforms import Form, SelectField, validators
...
... class MyForm(Form):
... visitor_timer = SelectField(
... choices=[('1', 'One')],
... )
...
... form = MyForm(data={'visitor_timer': '5'})
... print(form.validate())
False
Or if Optional
is kept but the form is filled with formdata
instead of data
:
>>> from wtforms import Form, SelectField, validators
... from werkzeug.datastructures import ImmutableMultiDict
...
... class MyForm(Form):
... visitor_timer = SelectField(
... validators=[validators.Optional()],
... choices=[('1', 'One')],
... )
...
... form = MyForm(ImmutableMultiDict({'visitor_timer': '5'}))
... print(form.validate())
False
I think what is going on is: when the form is initialized with data
instead of formdata
, Optional
looks for data in formdata
, considers there is no data there, so it interrupts the validation chain:
wtforms/src/wtforms/validators.py
Lines 249 to 256 in e205a99
What is your real-world usecase? Do you really use form validation without passing form data, or was it just there to fill the bug report?
from wtforms.
Hello,
I use data= instead of formdata= because I'm using wtforms to validate data received through my api created with Flask-Restfull.
Then I receive the request data through:
data = request.get_json()
form = MyForm(data=data)
And I do validation and so on...
Based on your answer, my question now is:
If I use
form = MyForm(ImmutableMultiDict(data))
will this work correctly?
from wtforms.
@HK-Mattew For your example form this will work but it will not work for all the possible field types since formdata
is inherently flat and consists of plain strings and uploaded files, while object data can be of an arbitrary type and can also be nested through use of things like FieldList
and FormField
. How the corresponding formdata
should look like compared to the object data will depend on the specific field, but it's not a trivial conversion.
But I also don't really understand your use-case, how are you generating the JSON payload? Or is your goal to provide both a generic API interface and a renderable form from a single form definition and get shared validation regardless of input method? Something like pydantic should be better suited towards generic input validation. Generating a wtforms.Form
class from a pydantic.Model
should be a much easier thing to implement.
from wtforms.
Hi @Daverball ,
I'm generating the raunchy JSON payload from javascript. In this case I create the object. I already use Pydantic models, but I didn't think about putting them in the foreground to validate the data. But it's a good option you gave me.
from wtforms.
@HK-Mattew In that case couldn't you just submit the raw formdata, rather than try to create a JSON payload? It seems like a lot of work to juggle around the backend data representation in the frontend if ultimately you just want to validate the form input.
from wtforms.
Related Issues (20)
- Feature request: parsing DateTimeField input with a function HOT 1
- QueryString: empty string is not validated like string with length > 1 HOT 1
- Email field return invalid email when whitespaces are available
- Got an unexpected keyword argument 'extra_filters' HOT 4
- DateField validation is broken HOT 1
- form.field.data= doesn't work anymore HOT 1
- Button submit HOT 2
- Security contact for vulnerability report HOT 1
- Error when a field is named `meta` HOT 4
- HTML `readonly` attribute support
- Document filters
- Multiple fields support HOT 1
- `html_params` does not work correctly for `Markup` values HOT 1
- Package metadata is incorrect HOT 1
- Failure/error in rendering the Field value (DecimalField) HOT 4
- Should forms be validated without request data? HOT 2
- Incorrect docstring? HOT 9
- ValueError: not enough values to unpack (expected 4, got 3) HOT 1
- IntegerFiled property can not pass the InputRequired or DataRequired validator when it is 0 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wtforms.