summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2015-10-05 14:08:48 (GMT)
committerAlex Pott2015-10-05 14:08:48 (GMT)
commit1c3bfd3bbd163e040e092e6a0023cd9ecea45456 (patch)
treee25aec6799de25dbc048b39e354ad4b9d3436415
parent72b0180f596e54643d8458d72d552ace0413cb22 (diff)
Issue #1218814 by swentel, pixelmord, andypost, franz: PDOException because of incorrect validation of number fields
-rw-r--r--core/lib/Drupal/Core/Field/Plugin/Field/FieldType/DecimalItem.php19
-rw-r--r--core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/RegexConstraint.php32
-rw-r--r--core/modules/field/src/Tests/Number/NumberFieldTest.php8
-rw-r--r--core/modules/field/src/Tests/Number/NumberItemTest.php3
4 files changed, 62 insertions, 0 deletions
diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/DecimalItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/DecimalItem.php
index c17e725..31cf07d 100644
--- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/DecimalItem.php
+++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/DecimalItem.php
@@ -95,6 +95,25 @@ class DecimalItem extends NumericItemBase {
/**
* {@inheritdoc}
*/
+ public function getConstraints() {
+ $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
+ $constraints = parent::getConstraints();
+
+ $constraints[] = $constraint_manager->create('ComplexData', array(
+ 'value' => array(
+ 'Regex' => array(
+ 'pattern' => '/^[+-]?((\d+(\.\d*)?)|(\.\d+))$/i',
+ )
+ ),
+ ));
+
+ return $constraints;
+ }
+
+
+ /**
+ * {@inheritdoc}
+ */
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$element = parent::fieldSettingsForm($form, $form_state);
$settings = $this->getSettings();
diff --git a/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/RegexConstraint.php b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/RegexConstraint.php
new file mode 100644
index 0000000..7d476eb
--- /dev/null
+++ b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/RegexConstraint.php
@@ -0,0 +1,32 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Core\Validation\Plugin\Validation\Constraint\RegexConstraint.
+ */
+
+namespace Drupal\Core\Validation\Plugin\Validation\Constraint;
+
+use Symfony\Component\Validator\Constraints\Regex;
+
+/**
+ * Regex constraint.
+ *
+ * Overrides the symfony constraint to use Drupal-style replacement patterns.
+ *
+ * @Constraint(
+ * id = "Regex",
+ * label = @Translation("Regex", context = "Validation")
+ * )
+ */
+class RegexConstraint extends Regex {
+
+ public $message = 'This value is not valid.';
+
+ /**
+ * Overrides Range::validatedBy().
+ */
+ public function validatedBy() {
+ return '\Symfony\Component\Validator\Constraints\RegexValidator';
+ }
+}
diff --git a/core/modules/field/src/Tests/Number/NumberFieldTest.php b/core/modules/field/src/Tests/Number/NumberFieldTest.php
index e4f104e..f80dfab 100644
--- a/core/modules/field/src/Tests/Number/NumberFieldTest.php
+++ b/core/modules/field/src/Tests/Number/NumberFieldTest.php
@@ -220,6 +220,14 @@ class NumberFieldTest extends WebTestBase {
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->assertRaw(t('%name must be lower than or equal to %maximum.', array('%name' => $field_name, '%maximum' => $maximum)), 'Correctly failed to save integer value greater than maximum allowed value.');
+ // Try to set a wrong integer value.
+ $this->drupalGet('entity_test/add');
+ $edit = array(
+ "{$field_name}[0][value]" => '20-40',
+ );
+ $this->drupalPostForm(NULL, $edit, t('Save'));
+ $this->assertRaw(t('%name must be a number.', array('%name' => $field_name)), 'Correctly failed to save wrong integer value.');
+
// Test with valid entries.
$valid_entries = array(
'-1234',
diff --git a/core/modules/field/src/Tests/Number/NumberItemTest.php b/core/modules/field/src/Tests/Number/NumberItemTest.php
index 4390449..e7eac30 100644
--- a/core/modules/field/src/Tests/Number/NumberItemTest.php
+++ b/core/modules/field/src/Tests/Number/NumberItemTest.php
@@ -53,6 +53,9 @@ class NumberItemTest extends FieldUnitTestBase {
$entity->field_integer = $integer;
$float = 3.14;
$entity->field_float = $float;
+ $entity->field_decimal = '20-40';
+ $violations = $entity->validate();
+ $this->assertIdentical(1, count($violations), 'Wrong decimal value causes validation error');
$decimal = '31.3';
$entity->field_decimal = $decimal;
$entity->name->value = $this->randomMachineName();