Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[3.0] [Form] IntegerType accepts floats - a bit misleading #10240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jameshalsall opened this issue Feb 11, 2014 · 10 comments
Closed

[3.0] [Form] IntegerType accepts floats - a bit misleading #10240

jameshalsall opened this issue Feb 11, 2014 · 10 comments

Comments

@jameshalsall
Copy link
Contributor

The IntegerType supports floats, but as the name suggests it should only accept integers, i.e. whole numbers.

The NumberType supports floats, locale specific numeric strings (e.g. "40,000") etc.

I propose a change to these 2 types:

  • IntegerType: should be used for whole integers as the name suggests
  • NumberType: should be used for floats (rendering as an input[type="number"]), and have a new option (support_locale_strings or something similar) so that it renders as input[type="text"] and has the relevant attached NumberToLocalizedStringTransformer.

This is obviously a BC break so marked it for 3.0.

@jakzal jakzal added the Form label Feb 11, 2014
@jameshalsall
Copy link
Contributor Author

I also noticed an issue with this today where you cannot render an input[type="number"] which supports decimals using the Form component.

The HTML5 number input does support decimals, and this should be reflected in either the NumberType or the IntegerType. It obviously makes far more sense with the NumberType, and I propose adding a new option to this form type to render it as a strict number input type (strict maybe?)

ping @webmozart

@Tobion
Copy link
Contributor

Tobion commented Feb 28, 2014

See #8106

@ioleo
Copy link

ioleo commented Nov 5, 2014

Just adding my opionion about the validation rules:

An integer is an integer. Every integer type should have validation constraints added by default, not allowing non integer input. Otherwise there is no benefit over useing a regular text field and adding custom validation constraints.

@ghost
Copy link

ghost commented Jun 24, 2015

The symfony form number type should also have a "step" option that would than be mapped to the html5 input type="number" step option. Than it would e.g. be possible to have float numbers that would only be a full number or a .5 number with step=".5"

@AxxiD
Copy link

AxxiD commented May 19, 2016

Another year has passed and still nothing has happend here.

I can confirm that NumberType renders to <input type="text" ... > and not as expected to <input type="number" ... > in version 3.0.6.

At this point this might be worth noting: /src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig#L135
The comment was added in 4c0d53a back in 2011. Since then things have changed and I'd say the statement is wrong. The type="number" definitely can handle floats. It also has this neat attribute called 'step' as mentioned by @ghost almost a year ago.

I'd love to see this corrected in the next version because especially on smartphones it's more convenient to have it as type="number" instead of plain text.

Also see #8106 which basically states the same as this issue.

@stof
Copy link
Member

stof commented May 19, 2016

@ghost step can already be added through attr

@AxxiD
Copy link

AxxiD commented May 19, 2016

@stof yes sure. But if NumberType would render into type="number" then it'd be nice to have a default value like step="any".

@jameshalsall
Copy link
Contributor Author

I think the field types have moved on slightly since this was opened, but now there is the issue of IntegerType supporting non-integer values when it would make more sense to have this support in a NumberType instead (with the localised string support like I mentioned in my original comment).

I doubt this will ever be changed now as it would break BC

@fabpot fabpot removed this from the 3.0 milestone Oct 18, 2016
@barryvdh
Copy link
Contributor

Isn't it an option to add a widget option for the NumberType (similar to DateType), with a default of text, but add an option for number. That could be without BC breaks, right?

@phavlik
Copy link

phavlik commented Dec 6, 2018

Just came across this issue. It really doesn't make any sense to me that IntegerType allows decimal numbers and there is no flag to disable it. Who thinks that 1.23 is integer?

nicolas-grekas added a commit that referenced this issue Feb 23, 2019
…xabbuh)

This PR was merged into the 3.4 branch.

Discussion
----------

[Form] IntegerType: reject submitted non-integer numbers

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #10240
| License       | MIT
| Doc PR        |

Commits
-------

6a43e74 IntegerType: reject submitted non-integer numbers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests