Git Product home page Git Product logo

Comments (4)

azmeuk avatar azmeuk commented on May 25, 2024

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.

HK-Mattew avatar HK-Mattew commented on May 25, 2024

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.

azmeuk avatar azmeuk commented on May 25, 2024

:param places:
How many decimal places to quantize the value to for display on form.
If unset, use 2 decimal places.
If explicitely set to `None`, does not quantize value.

Just set places to 3 or None and your snippet works.

from wtforms.

HK-Mattew avatar HK-Mattew commented on May 25, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.