Type

Type

Validates that a value is of a specific data type. For example, if a variable should be an array, you can use this constraint with the array type option to validate this.

Applies toproperty or method
Options
ClassSymfony\Component\Validator\Constraints\Type
ValidatorSymfony\Component\Validator\Constraints\TypeValidator

Basic Usage

This will check if emailAddress is an instance of Symfony\Component\Mime\Address, firstName is of type string (using [is_string](https://www.php.net/manual/en/function.is-string.php "is_string") PHP function), age is an integer (using [is_int](https://www.php.net/manual/en/function.is-int.php "is_int") PHP function) and accessCode contains either only letters or only digits (using [ctype_alpha](https://www.php.net/manual/en/function.ctype-alpha.php "ctype_alpha") and [ctype_digit](https://www.php.net/manual/en/function.ctype-digit.php "ctype_digit") PHP functions).

  • Annotations

    1. // src/Entity/Author.php
    2. namespace App\Entity;
    3. use Symfony\Component\Validator\Constraints as Assert;
    4. class Author
    5. {
    6. /**
    7. * @Assert\Type("Symfony\Component\Mime\Address")
    8. */
    9. protected $emailAddress;
    10. /**
    11. * @Assert\Type("string")
    12. */
    13. protected $firstName;
    14. /**
    15. * @Assert\Type(
    16. * type="integer",
    17. * message="The value {{ value }} is not a valid {{ type }}."
    18. * )
    19. */
    20. protected $age;
    21. /**
    22. * @Assert\Type(type={"alpha", "digit"})
    23. */
    24. protected $accessCode;
    25. }
  • YAML

    1. # config/validator/validation.yaml
    2. App\Entity\Author:
    3. properties:
    4. emailAddress:
    5. - Type: Symfony\Component\Mime\Address
    6. firstName:
    7. - Type: string
    8. age:
    9. - Type:
    10. type: integer
    11. message: The value {{ value }} is not a valid {{ type }}.
    12. accessCode:
    13. - Type:
    14. type: [alpha, digit]
  • XML

    1. <!-- config/validator/validation.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
    6. <class name="App\Entity\Author">
    7. <property name="emailAddress">
    8. <constraint name="Type">
    9. <option name="type">Symfony\Component\Mime\Address</option>
    10. </constraint>
    11. </property>
    12. <property name="firstName">
    13. <constraint name="Type">
    14. <option name="type">string</option>
    15. </constraint>
    16. </property>
    17. <property name="age">
    18. <constraint name="Type">
    19. <option name="type">integer</option>
    20. <option name="message">The value {{ value }} is not a valid {{ type }}.</option>
    21. </constraint>
    22. </property>
    23. <property name="accessCode">
    24. <constraint name="Type">
    25. <option name="type">
    26. <value>alpha</value>
    27. <value>digit</value>
    28. </option>
    29. </constraint>
    30. </property>
    31. </class>
    32. </constraint-mapping>
  • PHP

    1. // src/Entity/Author.php
    2. namespace App\Entity;
    3. use Symfony\Component\Mime\Address;
    4. use Symfony\Component\Validator\Constraints as Assert;
    5. use Symfony\Component\Validator\Mapping\ClassMetadata;
    6. class Author
    7. {
    8. public static function loadValidatorMetadata(ClassMetadata $metadata)
    9. {
    10. $metadata->addPropertyConstraint('emailAddress', new Assert\Type(Address::class));
    11. $metadata->addPropertyConstraint('firstName', new Assert\Type('string'));
    12. $metadata->addPropertyConstraint('age', new Assert\Type([
    13. 'type' => 'integer',
    14. 'message' => 'The value {{ value }} is not a valid {{ type }}.',
    15. ]));
    16. $metadata->addPropertyConstraint('accessCode', new Assert\Type([
    17. 'type' => ['alpha', 'digit'],
    18. ]));
    19. }
    20. }

New in version 4.4: The feature to define multiple types in the type option was introduced in Symfony 4.4.

Note

As with most of the other constraints, null is considered a valid value. This is to allow the use of optional values. If the value is mandatory, a common solution is to combine this constraint with NotNull.

Options

groups

type: array | string

It defines the validation group or groups this constraint belongs to. Read more about validation groups.

message

type: string default: This value should be of type {{ type }}.

The message if the underlying data is not of the given type.

You can use the following parameters in this message:

ParameterDescription
{{ type }}The expected type
{{ value }}The current (invalid) value

payload

type: mixed default: null

This option can be used to attach arbitrary domain-specific data to a constraint. The configured payload is not used by the Validator component, but its processing is completely up to you.

For example, you may want to use several error levels to present failed constraints differently in the front-end depending on the severity of the error.

type

type: string or array [default option]

New in version 4.4: The feature to define multiple types in the type option was introduced in Symfony 4.4.

This required option defines the type or collection of types allowed for the given value. Each type is either the FQCN (fully qualified class name) of some PHP class/interface or a valid PHP datatype (checked by PHP’s is_() functions):

  • [array](https://www.php.net/manual/en/function.is-array.php "is_array")
  • [bool](https://www.php.net/manual/en/function.is-bool.php "is_bool")
  • [callable](https://www.php.net/manual/en/function.is-callable.php "is_callable")
  • [float](https://www.php.net/manual/en/function.is-float.php "is_float")
  • [double](https://www.php.net/manual/en/function.is-double.php "is_double")
  • [int](https://www.php.net/manual/en/function.is-int.php "is_int")
  • [integer](https://www.php.net/manual/en/function.is-integer.php "is_integer")
  • [iterable](https://www.php.net/manual/en/function.is-iterable.php "is_iterable")
  • [long](https://www.php.net/manual/en/function.is-long.php "is_long")
  • [null](https://www.php.net/manual/en/function.is-null.php "is_null")
  • [numeric](https://www.php.net/manual/en/function.is-numeric.php "is_numeric")
  • [object](https://www.php.net/manual/en/function.is-object.php "is_object")
  • [real](https://www.php.net/manual/en/function.is-real.php "is_real")
  • [resource](https://www.php.net/manual/en/function.is-resource.php "is_resource")
  • [scalar](https://www.php.net/manual/en/function.is-scalar.php "is_scalar")
  • [string](https://www.php.net/manual/en/function.is-string.php "is_string")

Also, you can use ctype_*() functions from corresponding built-in PHP extension. Consider a list of ctype functions:

  • [alnum](https://www.php.net/manual/en/function.ctype-alnum.php "ctype_alnum")
  • [alpha](https://www.php.net/manual/en/function.ctype-alpha.php "ctype_alpha")
  • [cntrl](https://www.php.net/manual/en/function.ctype-cntrl.php "ctype_cntrl")
  • [digit](https://www.php.net/manual/en/function.ctype-digit.php "ctype_digit")
  • [graph](https://www.php.net/manual/en/function.ctype-graph.php "ctype_graph")
  • [lower](https://www.php.net/manual/en/function.ctype-lower.php "ctype_lower")
  • [print](https://www.php.net/manual/en/function.ctype-print.php "ctype_print")
  • [punct](https://www.php.net/manual/en/function.ctype-punct.php "ctype_punct")
  • [space](https://www.php.net/manual/en/function.ctype-space.php "ctype_space")
  • [upper](https://www.php.net/manual/en/function.ctype-upper.php "ctype_upper")
  • [xdigit](https://www.php.net/manual/en/function.ctype-xdigit.php "ctype_xdigit")

Make sure that the proper [locale](https://www.php.net/manual/en/function.setlocale.php "setlocale") is set before using one of these.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.