Skip to content

[Validator] Code property can't be populated to ConstraintViolation #7276

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
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Validator/Constraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ abstract class Constraint
*/
public $groups = array(self::DEFAULT_GROUP);

/**
* @var mixed
*/
public $code;

/**
* Initializes the constraint with options.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function validate($value, Constraint $constraint)
'{{ value }}' => $this->valueToString($constraint->value),
'{{ compared_value }}' => $this->valueToString($constraint->value),
'{{ compared_value_type }}' => $this->valueToType($constraint->value)
));
), $constraint->value, null, $constraint::ERROR);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Blank.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@
*/
class Blank extends Constraint
{
const ERROR = '3dfbbd3d-faed-46de-b781-fb1c3c9bbac3';

public $message = 'This value should be blank.';
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class BlankValidator extends ConstraintValidator
public function validate($value, Constraint $constraint)
{
if ('' !== $value && null !== $value) {
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
$this->context->addViolation($constraint->message, array('{{ value }}' => $value), $value, null, $constraint::ERROR);
}
}
}
2 changes: 2 additions & 0 deletions src/Symfony/Component/Validator/Constraints/CardScheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/
class CardScheme extends Constraint
{
const ERROR = '300f90d4-23d9-4bfe-a922-ebecff115e9d';

public $message = 'Unsupported card type or invalid card number.';
public $schemes;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function validate($value, Constraint $constraint)
}

if (!is_numeric($value)) {
$this->context->addViolation($constraint->message);
$this->context->addViolation($constraint->message, array(), $value, null, $constraint::ERROR);

return;
}
Expand All @@ -124,6 +124,6 @@ public function validate($value, Constraint $constraint)
}
}

$this->context->addViolation($constraint->message);
$this->context->addViolation($constraint->message, array(), $value, null, $constraint::ERROR);
}
}
5 changes: 5 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Choice.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
*/
class Choice extends Constraint
{
const ERROR = '75d601e8-d9da-49a6-bf32-6df461244744';
const ERROR_MULTIPLE = '695ad03b-1cdc-4b97-81d2-60c7de8424b5';
const ERROR_MIN = '34f58897-1e70-4353-97d7-27ebd472406e';
const ERROR_MAX = '73311798-3160-48b9-b71a-1502ba9e45d6';

public $choices;
public $callback;
public $multiple = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,25 @@ public function validate($value, Constraint $constraint)
if ($constraint->multiple) {
foreach ($value as $_value) {
if (!in_array($_value, $choices, $constraint->strict)) {
$this->context->addViolation($constraint->multipleMessage, array('{{ value }}' => $_value));
$this->context->addViolation($constraint->multipleMessage, array('{{ value }}' => $_value), $_value, null, $constraint::ERROR_MULTIPLE);
}
}

$count = count($value);

if ($constraint->min !== null && $count < $constraint->min) {
$this->context->addViolation($constraint->minMessage, array('{{ limit }}' => $constraint->min), null, (int) $constraint->min);
$this->context->addViolation($constraint->minMessage, array('{{ limit }}' => $constraint->min), null, (int) $constraint->min, $constraint::ERROR_MIN);

return;
}

if ($constraint->max !== null && $count > $constraint->max) {
$this->context->addViolation($constraint->maxMessage, array('{{ limit }}' => $constraint->max), null, (int) $constraint->max);
$this->context->addViolation($constraint->maxMessage, array('{{ limit }}' => $constraint->max), null, (int) $constraint->max, $constraint::ERROR_MAX);

return;
}
} elseif (!in_array($value, $choices, $constraint->strict)) {
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
$this->context->addViolation($constraint->message, array('{{ value }}' => $value), $value, null, $constraint::ERROR);
}
}
}
5 changes: 3 additions & 2 deletions src/Symfony/Component/Validator/Constraints/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Required;
use Symfony\Component\Validator\Constraints\Optional;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;

/**
Expand All @@ -25,6 +23,9 @@
*/
class Collection extends Constraint
{
const ERROR_EXTRA_FIELD = 'd3a68ca4-4836-42d9-a2c8-c21103a18b3f';
const ERROR_MISSING_FIELD = 'fb668c0b-5d90-4bda-8a3e-1fe4f1d5335b';

public $fields;
public $allowExtraFields = false;
public $allowMissingFields = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Constraints\Optional;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
Expand Down Expand Up @@ -50,7 +49,7 @@ public function validate($value, Constraint $constraint)
} elseif (!$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) {
$this->context->addViolationAt('['.$field.']', $constraint->missingFieldsMessage, array(
'{{ field }}' => $field
), null);
), $field, $constraint::ERROR_MISSING_FIELD);
}
}

Expand All @@ -59,7 +58,7 @@ public function validate($value, Constraint $constraint)
if (!isset($constraint->fields[$field])) {
$this->context->addViolationAt('['.$field.']', $constraint->extraFieldsMessage, array(
'{{ field }}' => $field
), $fieldValue);
), $field, null, $constraint::ERROR_EXTRA_FIELD);
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
*/
class Count extends Constraint
{
const ERROR_MIN = '4b57ee81-3a6b-4b43-93ae-69441571d39e';
const ERROR_MAX = '9537c665-37eb-4515-a1fd-7beff83e6c1f';
const ERROR_EXACT = 'd3625fef-ce0a-4332-a89e-408078ed6e58';

public $minMessage = 'This collection should contain {{ limit }} element or more.|This collection should contain {{ limit }} elements or more.';
public $maxMessage = 'This collection should contain {{ limit }} element or less.|This collection should contain {{ limit }} elements or less.';
public $exactMessage = 'This collection should contain exactly {{ limit }} element.|This collection should contain exactly {{ limit }} elements.';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function validate($value, Constraint $constraint)
$this->context->addViolation($constraint->exactMessage, array(
'{{ count }}' => $count,
'{{ limit }}' => $constraint->min,
), $value, (int) $constraint->min);
), $value, (int) $constraint->min, $constraint::ERROR_EXACT);

return;
}
Expand All @@ -48,7 +48,7 @@ public function validate($value, Constraint $constraint)
$this->context->addViolation($constraint->maxMessage, array(
'{{ count }}' => $count,
'{{ limit }}' => $constraint->max,
), $value, (int) $constraint->max);
), $value, (int) $constraint->max, $constraint::ERROR_MAX);

return;
}
Expand All @@ -57,7 +57,7 @@ public function validate($value, Constraint $constraint)
$this->context->addViolation($constraint->minMessage, array(
'{{ count }}' => $count,
'{{ limit }}' => $constraint->min,
), $value, (int) $constraint->min);
), $value, (int) $constraint->min, $constraint::ERROR_MIN);
}
}
}
2 changes: 2 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@
*/
class Country extends Constraint
{
const ERROR = '326b7f14-6fde-4489-8e9b-8458ecc2e559';

public $message = 'This value is not a valid country.';
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function validate($value, Constraint $constraint)
$countries = Intl::getRegionBundle()->getCountryNames();

if (!isset($countries[$value])) {
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
$this->context->addViolation($constraint->message, array('{{ value }}' => $value), $value, null, $constraint::ERROR);
}
}
}
2 changes: 2 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@
*/
class Currency extends Constraint
{
const ERROR = 'f68a8d40-4231-4afd-83ac-0bcd818dc818';

public $message = 'This value is not a valid currency.';
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function validate($value, Constraint $constraint)
$currencies = Intl::getCurrencyBundle()->getCurrencyNames();

if (!isset($currencies[$value])) {
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
$this->context->addViolation($constraint->message, array('{{ value }}' => $value), $value, null, $constraint::ERROR);
}
}
}
2 changes: 2 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@
*/
class Date extends Constraint
{
const ERROR = 'f360bec2-4636-46ad-8ef6-aecc65e08a00';

public $message = 'This value is not a valid date.';
}
2 changes: 2 additions & 0 deletions src/Symfony/Component/Validator/Constraints/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@
*/
class DateTime extends Constraint
{
const ERROR = '383654a2-f53e-4529-ace3-176742663f3a';

public $message = 'This value is not a valid datetime.';
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function validate($value, Constraint $constraint)
$value = (string) $value;

if (!preg_match(static::PATTERN, $value, $matches) || !checkdate($matches[2], $matches[3], $matches[1])) {
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
$this->context->addViolation($constraint->message, array('{{ value }}' => $value), $value, null, $constraint::ERROR);
}
}
}
2 changes: 2 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/
class Email extends Constraint
{
const ERROR = '38b1dcdc-501a-43c1-98e1-4ab0971ca1b2';

public $message = 'This value is not a valid email address.';
public $checkMX = false;
public $checkHost = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function validate($value, Constraint $constraint)
}

if (!$valid) {
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
$this->context->addViolation($constraint->message, array('{{ value }}' => $value), $value, null, $constraint::ERROR);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Validator/Constraints/EqualTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@
*/
class EqualTo extends AbstractComparison
{
const ERROR = 'f92206ec-df92-406d-931b-833343889cd7';

public $message = 'This value should be equal to {{ compared_value }}.';
}
2 changes: 2 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
class Expression extends Constraint
{
const ERROR = '2fe78c81-b47e-4d20-952b-f571a301a8df';

public $message = 'This value is not valid.';
public $expression;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function validate($value, Constraint $constraint)
}

if (!$this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) {
$this->context->addViolation($constraint->message);
$this->context->addViolation($constraint->message, array('{{ value }}' => $value), $value, null, $constraint::ERROR);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Validator/Constraints/False.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@
*/
class False extends Constraint
{
const ERROR = '890770b4-e5c4-49e5-a285-b480ab42b43a';

public $message = 'This value should be false.';
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public function validate($value, Constraint $constraint)
return;
}

$this->context->addViolation($constraint->message);
$this->context->addViolation($constraint->message, array(), $value, null, $constraint::ERROR);
}
}
14 changes: 14 additions & 0 deletions src/Symfony/Component/Validator/Constraints/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@
*/
class File extends Constraint
{
const ERROR_NOT_FOUND = '61fbc820-360e-4635-8410-6e18c55d0793';
const ERROR_NOT_READABLE = '27fb8e2a-e615-460a-8248-2b9fd7fb0f03';
const ERROR_MAX_SIZE = '94ab2b21-4bb2-4f2c-8b0f-2f23ee3fa083';
const ERROR_MIME_TYPE = 'aeaf332e-eb9f-4f5b-954b-73a4ee10099b';

const ERROR_UPLOAD_INI_SIZE = '3ccb63bf-288f-4a83-9ded-750eb60efeb7';
const ERROR_UPLOAD_FORM_SIZE = 'd8af3119-4ca1-4b68-b6a8-742fd1713dfb';
const ERROR_UPLOAD_PARTIAL = '9607840b-25a2-4a06-b6de-a443fdf7bc89';
const ERROR_UPLOAD_NO_FILE = '5025cb15-812e-411c-8837-cae3106dec7c';
const ERROR_UPLOAD_NO_TMP_DIR = '6507b3da-97db-4c89-9628-2b41b4772fed';
const ERROR_UPLOAD_CANT_WRITE = '3da80e51-9c7d-41ea-b077-7385633c9bbc';
const ERROR_UPLOAD_EXTENSION = 'fc050994-f85a-4226-b35f-7a7e55fe29d4';
const ERROR_UPLOAD = '4e786105-6f8b-42fa-a409-f29792fa297a';

public $maxSize = null;
public $mimeTypes = array();
public $notFoundMessage = 'The file could not be found.';
Expand Down
Loading