Skip to content

streamline the constraints reference #11366

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

Merged
merged 1 commit into from
Apr 9, 2019
Merged
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
2 changes: 1 addition & 1 deletion reference/constraints/All.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ entry in that array:
* @Assert\Length(min=5)
* })
*/
protected $favoriteColors = [];
protected $favoriteColors = [];
}

.. code-block:: yaml
Expand Down
2 changes: 0 additions & 2 deletions reference/constraints/Bic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ will contain a Business Identifier Code (BIC).

class Transaction
{
protected $businessIdentifierCode;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@OskarStark Wouldn't it be less confusing to always add the property instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ofc we can, but most of them does not have them.

You get my 👍


public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('businessIdentifierCode', new Assert\Bic());
Expand Down
5 changes: 5 additions & 0 deletions reference/constraints/Callback.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ Configuration
{
$metadata->addConstraint(new Assert\Callback('validate'));
}

public function validate(ExecutionContextInterface $context, $payload)
{
// ...
}
}

The Callback Method
Expand Down
2 changes: 0 additions & 2 deletions reference/constraints/CardScheme.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ on an object that will contain a credit card number.

class Transaction
{
protected $cardNumber;

public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('cardNumber', new Assert\CardScheme([
Expand Down
2 changes: 0 additions & 2 deletions reference/constraints/Choice.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ If your valid choice list is simple, you can pass them in directly via the

class Author
{
protected $genre;

public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint(
Expand Down
11 changes: 5 additions & 6 deletions reference/constraints/Collection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,6 @@ following:

class Author
{
private $options = [];

public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('profileData', new Assert\Collection([
Expand Down Expand Up @@ -208,7 +206,7 @@ you can do the following:
* }
* )
*/
protected $profileData = ['personal_email'];
protected $profileData = ['personal_email'];
}

.. code-block:: yaml
Expand Down Expand Up @@ -272,9 +270,10 @@ you can do the following:
{
$metadata->addPropertyConstraint('profileData', new Assert\Collection([
'fields' => [
'personal_email' => new Assert\Required(
[new Assert\NotBlank(), new Assert\Email()]
),
'personal_email' => new Assert\Required([
new Assert\NotBlank(),
new Assert\Email(),
]),
'alternate_email' => new Assert\Optional(new Assert\Email()),
],
]));
Expand Down
6 changes: 3 additions & 3 deletions reference/constraints/Count.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ you might add the following:
* maxMessage = "You cannot specify more than {{ limit }} emails"
* )
*/
protected $emails = [];
protected $emails = [];
}

.. code-block:: yaml
Expand Down Expand Up @@ -93,8 +93,8 @@ you might add the following:
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('emails', new Assert\Count([
'min' => 1,
'max' => 5,
'min' => 1,
'max' => 5,
'minMessage' => 'You must specify at least one email',
'maxMessage' => 'You cannot specify more than {{ limit }} emails',
]));
Expand Down
2 changes: 1 addition & 1 deletion reference/constraints/Country.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Basic Usage
/**
* @Assert\Country
*/
protected $country;
protected $country;
}

.. code-block:: yaml
Expand Down
2 changes: 1 addition & 1 deletion reference/constraints/Date.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Basic Usage
/**
* @Assert\Date
*/
protected $birthday;
protected $birthday;
}

.. code-block:: yaml
Expand Down
2 changes: 1 addition & 1 deletion reference/constraints/DateTime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Basic Usage
/**
* @Assert\DateTime
*/
protected $createdAt;
protected $createdAt;
}

.. code-block:: yaml
Expand Down
2 changes: 1 addition & 1 deletion reference/constraints/Email.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Basic Usage
* checkMX = true
* )
*/
protected $email;
protected $email;
}

.. code-block:: yaml
Expand Down
6 changes: 2 additions & 4 deletions reference/constraints/Image.rst
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,11 @@ following code:

class Author
{
// ...

public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('headshot', new Assert\Image([
'allowLandscape' => false,
'allowPortrait' => false,
'allowLandscape' => false,
'allowPortrait' => false,
]));
}
}
Expand Down
2 changes: 1 addition & 1 deletion reference/constraints/Ip.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Basic Usage
/**
* @Assert\Ip
*/
protected $ipAddress;
protected $ipAddress;
}

.. code-block:: yaml
Expand Down
15 changes: 11 additions & 4 deletions reference/constraints/IsFalse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ method returns **false**:
* message = "You've entered an invalid state."
* )
*/
public function isStateInvalid()
{
public function isStateInvalid()
{
// ...
}
}
}

.. code-block:: yaml
Expand Down Expand Up @@ -99,10 +99,17 @@ method returns **false**:
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addGetterConstraint('stateInvalid', new Assert\IsFalse());
$metadata->addGetterConstraint('stateInvalid', new Assert\IsFalse([
'message' => 'You've entered an invalid state.',
]));
}
}

public function isStateInvalid()
{
// ...
}

Options
-------

Expand Down
4 changes: 1 addition & 3 deletions reference/constraints/IsTrue.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Then you can constrain this method with ``IsTrue``.
protected $token;

/**
* @Assert\IsTrue(message="The token is invalid")
* @Assert\IsTrue(message="The token is invalid.")
*/
public function isTokenValid()
{
Expand Down Expand Up @@ -100,8 +100,6 @@ Then you can constrain this method with ``IsTrue``.

class Author
{
protected $token;

public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addGetterConstraint('tokenValid', new IsTrue([
Expand Down
4 changes: 1 addition & 3 deletions reference/constraints/Isbn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,10 @@ on an object that will contain an ISBN.

class Book
{
protected $isbn;

public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('isbn', new Assert\Isbn([
'type' => 'isbn10',
'type' => 'isbn10',
'message' => 'This value is not valid.',
]));
}
Expand Down
4 changes: 2 additions & 2 deletions reference/constraints/Issn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Basic Usage
/**
* @Assert\Issn
*/
protected $issn;
protected $issn;
}

.. code-block:: yaml
Expand Down Expand Up @@ -87,7 +87,7 @@ Options
message
~~~~~~~

**type**: ``String`` default: ``This value is not a valid ISSN.``
**type**: ``string`` default: ``This value is not a valid ISSN.``

The message shown if the given value is not a valid ISSN.

Expand Down
2 changes: 1 addition & 1 deletion reference/constraints/Language.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Basic Usage
/**
* @Assert\Language
*/
protected $preferredLanguage;
protected $preferredLanguage;
}

.. code-block:: yaml
Expand Down
6 changes: 3 additions & 3 deletions reference/constraints/Length.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ and "50", you might add the following:
* maxMessage = "Your first name cannot be longer than {{ limit }} characters"
* )
*/
protected $firstName;
protected $firstName;
}

.. code-block:: yaml
Expand Down Expand Up @@ -104,8 +104,8 @@ and "50", you might add the following:
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('firstName', new Assert\Length([
'min' => 2,
'max' => 50,
'min' => 2,
'max' => 50,
'minMessage' => 'Your first name must be at least {{ limit }} characters long',
'maxMessage' => 'Your first name cannot be longer than {{ limit }} characters',
]));
Expand Down
2 changes: 1 addition & 1 deletion reference/constraints/Locale.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Basic Usage
/**
* @Assert\Locale
*/
protected $locale;
protected $locale;
}

.. code-block:: yaml
Expand Down
4 changes: 2 additions & 2 deletions reference/constraints/Range.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ you might add the following:
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('height', new Assert\Range([
'min' => 120,
'max' => 180,
'min' => 120,
'max' => 180,
'minMessage' => 'You must be at least {{ limit }}cm tall to enter',
'maxMessage' => 'You cannot be taller than {{ limit }}cm to enter',
]));
Expand Down
4 changes: 2 additions & 2 deletions reference/constraints/Regex.rst
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ it a custom message:
{
$metadata->addPropertyConstraint('firstName', new Assert\Regex([
'pattern' => '/\d/',
'match' => false,
'match' => false,
'message' => 'Your name cannot contain a number',
]));
}
Expand Down Expand Up @@ -257,7 +257,7 @@ need to specify the HTML5 compatible pattern in the ``htmlPattern`` option:
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('name', new Assert\Regex([
'pattern' => '/^[a-z]+$/i',
'pattern' => '/^[a-z]+$/i',
'htmlPattern' => '^[a-zA-Z]+$',
]));
}
Expand Down
2 changes: 1 addition & 1 deletion reference/constraints/Time.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ of the day when the event starts:
/**
* @Assert\Time
*/
protected $startsAt;
protected $startsAt;
}

.. code-block:: yaml
Expand Down
2 changes: 1 addition & 1 deletion reference/constraints/Type.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ This will check if ``firstName`` is of type ``string`` and that ``age`` is an
$metadata->addPropertyConstraint('firstName', new Assert\Type('string'));

$metadata->addPropertyConstraint('age', new Assert\Type([
'type' => 'integer',
'type' => 'integer',
'message' => 'The value {{ value }} is not a valid {{ type }}.',
]));
}
Expand Down
10 changes: 3 additions & 7 deletions reference/constraints/UniqueEntity.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,10 @@ your user table:
class Author
{
/**
* @var string $email
*
* @ORM\Column(name="email", type="string", length=255, unique=true)
* @Assert\Email
*/
protected $email;

// ...
}

.. code-block:: yaml
Expand Down Expand Up @@ -104,7 +100,7 @@ your user table:
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addConstraint(new UniqueEntity([
'fields' => 'email',
'fields' => 'email',
]));

$metadata->addPropertyConstraint('email', new Assert\Email());
Expand Down Expand Up @@ -285,9 +281,9 @@ Consider this example:
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addConstraint(new UniqueEntity([
'fields' => ['host', 'port'],
'fields' => ['host', 'port'],
'errorPath' => 'port',
'message' => 'This port is already in use on that host.',
'message' => 'This port is already in use on that host.',
]));
}
}
Expand Down
Loading