Comments (4)
Hi
Running python 3.8.18 on the 3.0.1 tag, and copy/pasting your snippet, I cannot reproduce this bug, and the value I see 0.03
. I am not sure what the difference come from.
Are you sure this snippet executes "as is"? Did you try it on other execution contexts? (different python version, different OS etc?)
from wtforms.
I was racking my brain to find out the real reason this failure happened and I found it. See my code for explanations.
from wtforms import Form, DecimalField, validators
MIN_CPC = 0.0035
class MyForm(Form):
#CPC Part
cpc = DecimalField(
label='CPC',
validators=[
validators.DataRequired(),
validators.NumberRange(
min=MIN_CPC
)
],
render_kw={
'placeholder': f'Min: {MIN_CPC} USD'
}
)
"""
[ Works correctly ]:
"""
form = MyForm(data={'cpc': 0.01})
print('cpc from form: ', form.cpc.data)
#Result: cpc from form: 0.01
print(form.cpc.__html__())
# Result: <input id="cpc" min="0.0035" name="cpc" placeholder="Min: 0.0035 USD" required step="any" type="number" value="0.01">
"""
[ Rendering failed ]:
"""
form = MyForm(data={'cpc': 0.001}) # * Values with more than 2 decimal places. *
print('cpc from form: ', form.cpc.data)
#Result: cpc from form: 0.001
print(form.cpc.__html__())
# Result: <input id="cpc" min="0.0035" name="cpc" placeholder="Min: 0.0035 USD" required step="any" type="number" value="0.00">
"""
The failure happens when I enter values with more than 2 decimal places.
*form.cpc.data* works correctly.
*form.cpc.__html__()* Does not work correctly.
"""
from wtforms.
wtforms/src/wtforms/fields/numeric.py
Lines 99 to 102 in e205a99
Just set places
to 3 or None and your snippet works.
from wtforms.
That worked.
But I just want to leave my opinion. In my opinion, I think this option should be None by default. Because if Field has decimal values, I believe that it should not reduce the decimal places of the values to 2 by default. Well, I don't see any sense in that. (But I may be wrong. I honestly don't know why this is active as 2 by default).
from wtforms.
Related Issues (20)
- 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
- Validation failure in SelectField HOT 8
- 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
- SelectMultipleField overrides default, keyword and obj provided values when empty post data is provided to the form HOT 4
- SelectMultipleField should support choices specified as coercible values. HOT 2
- Question: Is it possible to create a dynamic/adaptive forms with WTForms? HOT 2
- Missing period for two messages 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.