-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Added error codes to all constraints with multiple error causes #12021
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
namespace Symfony\Component\Validator; | ||
|
||
use Symfony\Component\Validator\Exception\ConstraintDefinitionException; | ||
use Symfony\Component\Validator\Exception\InvalidArgumentException; | ||
use Symfony\Component\Validator\Exception\InvalidOptionsException; | ||
use Symfony\Component\Validator\Exception\MissingOptionsException; | ||
|
||
|
@@ -50,12 +51,40 @@ abstract class Constraint | |
*/ | ||
const PROPERTY_CONSTRAINT = 'property'; | ||
|
||
/** | ||
* Maps error codes to the names of their constants | ||
* @var array | ||
*/ | ||
protected static $errorNames = array(); | ||
|
||
/** | ||
* Domain-specific data attached to a constraint | ||
* @var mixed | ||
*/ | ||
public $payload; | ||
|
||
/** | ||
* Returns the name of the given error code. | ||
* | ||
* @param int $errorCode The error code | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the error code can be mixed according to https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilderInterface.php#L96 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
* | ||
* @return string The name of the error code | ||
* | ||
* @throws InvalidArgumentException If the error code does not exist | ||
*/ | ||
public static function getErrorName($errorCode) | ||
{ | ||
if (!isset(static::$errorNames[$errorCode])) { | ||
throw new InvalidArgumentException(sprintf( | ||
'The error code "%s" does not exist for constraint of type "%s".', | ||
$errorCode, | ||
get_called_class() | ||
)); | ||
} | ||
|
||
return static::$errorNames[$errorCode]; | ||
} | ||
|
||
/** | ||
* Initializes the constraint with options. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,9 +20,18 @@ | |
* @Target({"PROPERTY", "METHOD", "ANNOTATION"}) | ||
* | ||
* @author Tim Nagel <[email protected]> | ||
* @author Bernhard Schussek <[email protected]> | ||
*/ | ||
class CardScheme extends Constraint | ||
{ | ||
const NOT_NUMERIC_ERROR = 1; | ||
const INVALID_FORMAT_ERROR = 2; | ||
|
||
protected static $errorNames = array( | ||
self::NOT_NUMERIC_ERROR => 'NOT_NUMERIC_ERROR', | ||
self::INVALID_FORMAT_ERROR => 'INVALID_FORMAT_ERROR', | ||
); | ||
|
||
public $message = 'Unsupported card type or invalid card number.'; | ||
public $schemes; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could it be private instead ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, no sorry, forget it, it overwrites the parent ones