# Validation API Platform take care of validating data sent to the API by the client (usually user data entered through forms). By default, the framework relies on [the powerful Symfony Validator Component](http://symfony.com/doc/current/validation.html) for this task, but you can replace it by your preferred validation library such as [the PHP filter extension](http://php.net/manual/en/intro.filter.php) if you want to. ## Validating Submitted Data Validating submitted data is simple as adding [Symfony's built-in constraints](http://symfony.com/doc/current/reference/constraints.html) or [custom constraints](http://symfony.com/doc/current/validation/custom_constraint.html) directly in classes marked with the `@ApiResource` annotation: ```php context->buildViolation($constraint->message)->addViolation(); } } } ``` If the data submitted by the client is invalid, the HTTP status code will be set to `400 Bad Request` and the response's body will contain the list of violations serialized in a format compliant with the requested one. For instance, a validation error will look like the following if the requested format is JSON-LD (the default): ```json { "@context": "/contexts/ConstraintViolationList", "@type": "ConstraintViolationList", "hydra:title": "An error occurred", "hydra:description": "properties: The product must have the minimal properties required (\"description\", \"price\")", "violations": [ { "propertyPath": "properties", "message": "The product must have the minimal properties required (\"description\", \"price\")" } ] } ``` Take a look at the [Errors Handling guide](errors.md) to learn how API Platform converts PHP exceptions like validation errors to HTTP errors. ## Using Validation Groups Without specific configuration, the default validation group is always used, but this behavior is customizable: the framework is able to leverage Symfony's [validation groups](http://symfony.com/doc/current/book/validation.html#validation-groups). You can configure the groups you want to use when the validation occurs directly through the `ApiResource` annotation: ```php authorizationChecker = $authorizationChecker; } public function __invoke(Book $book): array { return $this->authorizationChecker->isGranted('ROLE_ADMIN', $book) ? ['a', 'b'] : ['a']; } } ``` This class selects the groups to apply regarding the role of the current user: if the current user has the `ROLE_ADMIN` role, groups `a` and `b` are returned. In other cases, just `a` is returned. This class is automatically registered as a service thanks to [the autowiring feature of the Symfony Dependency Injection Component](https://symfony.com/doc/current/service_container/autowiring.html). Just note that this service must be public. Then, configure the entity class to use this service to retrieve validation groups: ```php