The validation of a number fields text widget allows for a '-' character not only at the beginning of the test representing the number (integer, decimal or float).

That allows values entered like "20-40", which is correct by the validation function of the widget but will throw an exception when trying to save to the database.

Problem here is the regular expression used for the validation.

Comments

pixelmord’s picture

StatusFileSize
new1010 bytes

Attached is a patch that uses preg_match instead of preg_replace and checks that a "-" can only be placed at the beginning of the string representing the number.

marcingy’s picture

Version: 7.4 » 8.x-dev
Status: Active » Needs review
Issue tags: +Needs backport to D7

Moving to d8 and marking for backport.

Status: Needs review » Needs work

The last submitted patch, number_field_validation-1218814-2.patch, failed testing.

pixelmord’s picture

StatusFileSize
new1.1 KB

Last patch was a diff relative to the number module directory, so attached is a patch relative to Drupal root of the latest 8.x-dev

marcingy’s picture

Status: Needs work » Needs review

Setting needs review for the bot.

Status: Needs review » Needs work

The last submitted patch, number_field_validation-1218814-4.patch, failed testing.

franz’s picture

Test fails because of:

Previously, the regexp didn't test for more than one decimal point, and that check was done later. Now that the regexp detects it earlier, the error message is not the expected one.

franz’s picture

StatusFileSize
new2.37 KB

It's not a pretty patch, not 100% happy with it, but I think it works with minimal changes on code.

franz’s picture

Status: Needs work » Needs review
franz’s picture

Title: PDOException beacause of incorrect validation of number fields » PDOException because of incorrect validation of number fields
pixelmord’s picture

Status: Needs review » Needs work

Yeah you are right, I overlooked that one, because I did not look at the test.

The question is, if the user really needs that level of detail in the error message, but since it does not really hurt to be more specific, the extra message regarding the amount of decimal points might be helpful.

pixelmord’s picture

Status: Needs work » Needs review

Sorry did not want to change Status.

It works for me

franz’s picture

I wouldn't mind dropping this specific error message at all, provided the tests are done for the same cases. But I lack powers to mess with it. =)

attiks’s picture

I created #1845546: Implement validation for the TypedData API to try to solve this on all levels, since there are only 10 days left, we need to figure out what needs to be done before feature freeze and what can be done later.

swentel’s picture

Component: field system » number.module
swentel’s picture

Issue summary: View changes

corrected a typo

jhedstrom’s picture

Issue summary: View changes
Status: Needs review » Needs work
Issue tags: +Needs reroll
yogen.prasad’s picture

Assigned: Unassigned » yogen.prasad
manjit.singh’s picture

Issue tags: +SprintWeekend2015
yogen.prasad’s picture

Assigned: yogen.prasad » Unassigned
andypost’s picture

Component: number.module » field system
Issue tags: +Needs tests

When browser does not support html5 validation or validation disabled http://stackoverflow.com/a/3094185
Core now stores empty value silently without validation.

So better to add validation to widget or maybe "number" element additionally to browser provided

andypost’s picture

Status: Needs work » Needs review
StatusFileSize
new1023 bytes

Looks now is_numberic check is enough in \Drupal\Core\Render\Element\Number::validateNumber

Here's a test to make sure that "20-40" is now checked fine

andypost’s picture

Issue tags: -Needs tests
StatusFileSize
new724 bytes

Here's a test that proves that decimal item by-passes validation

andypost’s picture

Issue tags: +Entity Field API

Status: Needs review » Needs work

The last submitted patch, 22: 1218814-22.patch, failed testing.

deepakaryan1988’s picture

Issue tags: -SprintWeekend2015

Removing sprint weekend tag!!
As suggested by @YesCT

rpayanm’s picture

Issue tags: -Needs reroll
swentel’s picture

Isn't decimal fine because the datatype is a string ?

swentel’s picture

Oh woops, didn't look at the schema, ignore my comment

swentel’s picture

Status: Needs work » Needs review
Issue tags: +DUGBE0609
StatusFileSize
new3.8 KB
new2.1 KB

And the fix - added a new constraint based on Symfony's Regex validator.

The last submitted patch, 29: 1218814-29.patch, failed testing.

andypost’s picture

+++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/DecimalItem.php
@@ -95,6 +95,25 @@ public function storageSettingsForm(array &$form, FormStateInterface $form_state
+          'pattern' => '/^[0-9]+(\.[0-9]{1,2})?$/i',

this should care about decimal and thousand separators

swentel’s picture

Assigned: Unassigned » swentel
swentel’s picture

StatusFileSize
new3.81 KB
new645 bytes

regex fix. Don't think we should account for comma's as the storage won't allow that IIRC - that's more for formatters at output imo.

swentel’s picture

Assigned: swentel » Unassigned
strykaizer’s picture

Status: Needs review » Reviewed & tested by the community

Patch applied and works as needed; Could't find any code issues so marking as rtbc.

alexpott’s picture

Version: 8.0.x-dev » 7.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

Adding a validator is low risk and this fixes a bug so is permitted during beta. Committed 1c3bfd3 and pushed to 8.0.x. Thanks!

  • alexpott committed 1c3bfd3 on 8.0.x
    Issue #1218814 by swentel, pixelmord, andypost, franz: PDOException...

  • alexpott committed 1c3bfd3 on 8.1.x
    Issue #1218814 by swentel, pixelmord, andypost, franz: PDOException...

  • alexpott committed 1c3bfd3 on 8.3.x
    Issue #1218814 by swentel, pixelmord, andypost, franz: PDOException...

  • alexpott committed 1c3bfd3 on 8.3.x
    Issue #1218814 by swentel, pixelmord, andypost, franz: PDOException...

  • alexpott committed 1c3bfd3 on 8.4.x
    Issue #1218814 by swentel, pixelmord, andypost, franz: PDOException...

  • alexpott committed 1c3bfd3 on 8.4.x
    Issue #1218814 by swentel, pixelmord, andypost, franz: PDOException...
poker10’s picture

This bug seems to be no longer present in D7 as it was fixed by #1068016: number field validation fails to block some invalid input causing later SQL fatal error. There is also a test (NumberFieldTestCase::testNumberDecimalField()) checking inputs like "3-3", "-10-10". I think this can be set as Fixed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.