Commit 1c3bfd3b authored by alexpott's avatar alexpott

Issue #1218814 by swentel, pixelmord, andypost, franz: PDOException because of...

Issue #1218814 by swentel, pixelmord, andypost, franz: PDOException because of incorrect validation of number fields
parent 72b0180f
......@@ -92,6 +92,25 @@ public function storageSettingsForm(array &$form, FormStateInterface $form_state
return $element;
}
/**
* {@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}
*/
......
<?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';
}
}
......@@ -220,6 +220,14 @@ function testNumberIntegerField() {
$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',
......
......@@ -53,6 +53,9 @@ public function testNumberItem() {
$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();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment