Skip to content
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

[Form] NumberType should display an error when a string is submitted #5707

Closed
bdmu opened this issue Oct 9, 2012 · 4 comments
Closed

[Form] NumberType should display an error when a string is submitted #5707

bdmu opened this issue Oct 9, 2012 · 4 comments

Comments

@bdmu
Copy link
Contributor

bdmu commented Oct 9, 2012

Hello,

In Symfony 2.1.1, when a string such '1aaa' is input in a 'number' Form field, there is no validation error and the string value '1aaa' is converted in the numeric value '1'.

It looks like it is due to NumberFormatter in NumberToLocalizedStringTransformer.reverseTransform

a small php script which reproduces the conversion from string to numeric:

setAttribute(\NumberFormatter::FRACTION_DIGITS, 3); $formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, \NumberFormatter::ROUND_HALFUP); $formatter->setAttribute(\NumberFormatter::GROUPING_USED, false); return $formatter; } $formatter = getNumberFormatter(); $value = $formatter->parse('1aaa'); echo "Error Code:".$formatter->getErrorCode()."\n"; echo "intl_is_failure:".intl_is_failure($formatter->getErrorCode())."\n"; echo "is_numeric:".is_numeric($value)."\n"; echo $value; => Error Code:0 intl_is_failure: is_numeric:1 1 Best regards, Christophe
@webmozart
Copy link
Contributor

Thank you for taking the time to report this issue! As you found out, NumberFormatter is the reason for this behavior. When #2059 is solved, this issue can probably be tackled as well.

@ericclemmons
Copy link
Contributor

I've been struggling with this as well when a bad value is put in, like foo.

Would it be acceptable to at least throw an exception in NumberToLocalizedStringTransformer if $value doesn't at least contain a single digit?

@webmozart
Copy link
Contributor

@ericclemmons I cannot reproduce your issue. When I enter foo or foo1, I get an error on the field. Only if I enter 1foo, the input is interpreted as 1.

@webmozart
Copy link
Contributor

This is fixed in the referenced PR.

fabpot added a commit that referenced this issue Nov 8, 2012
This PR was merged into the master branch.

Commits
-------

c16d6bf [Form] Fixed NumberToLocalizedStringTransformer to throw an exception if numbers contain illegal characters

Discussion
----------

[Form] Fixed NumberType to fail if numbers contain letters

Bug fix: yes
Feature addition: no
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: #5707
Todo: -
License of the code: MIT
Documentation PR: -
@fabpot fabpot closed this as completed Nov 8, 2012
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

4 participants