PHPackages                             sfmok/request-input-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. sfmok/request-input-bundle

ActiveSymfony-bundle[Validation &amp; Sanitization](/categories/validation)

sfmok/request-input-bundle
==========================

Converts request data into DTO inputs objects with validation.

v2.0.0(2mo ago)3512.8k↓100%1[2 issues](https://github.com/sfmok/request-input-bundle/issues)MITPHPPHP &gt;=8.4

Since Nov 9Pushed 2mo ago2 watchersCompare

[ Source](https://github.com/sfmok/request-input-bundle)[ Packagist](https://packagist.org/packages/sfmok/request-input-bundle)[ RSS](/packages/sfmok-request-input-bundle/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (10)Versions (12)Used By (0)

RequestInputBundle
------------------

[](#requestinputbundle)

[![CI](https://github.com/sfmok/request-input/actions/workflows/ci.yml/badge.svg)](https://github.com/sfmok/request-input/actions/workflows/ci.yml)[![codecov](https://camo.githubusercontent.com/e0e8ed5a8cb1ae2ad1d14ffcb875a55e3e15cae55e5305830b7242125936a751/68747470733a2f2f636f6465636f762e696f2f6769746875622f73666d6f6b2f726571756573742d696e7075742d62756e646c652f6272616e63682f6d61696e2f67726170682f62616467652e7376673f746f6b656e3d39454447525550594342)](https://codecov.io/github/sfmok/request-input-bundle)[![Latest Stable Version](https://camo.githubusercontent.com/a253beb72ae7630c4da8c107026ac61d595f86e5d0dc34b76ffa1fe918ceec5a/687474703a2f2f706f7365722e707567782e6f72672f73666d6f6b2f726571756573742d696e7075742d62756e646c652f762f737461626c65)](https://packagist.org/packages/sfmok/request-input-bundle)[![License](https://camo.githubusercontent.com/b2e751920a1112735d3d4bdecfa72fc9300d5ac82eea023fdd2fa9edfe59c5ea/687474703a2f2f706f7365722e707567782e6f72672f73666d6f6b2f726571756573742d696e7075742d62756e646c652f6c6963656e7365)](https://packagist.org/packages/sfmok/request-input-bundle)

**RequestInputBundle** converts request data into DTO inputs objects with validation.

- Request data supported: `json` and `xml` based on `Content-Type` header.
- Resolve inputs arguments for controllers actions.
- Create DTO inputs outside controllers
- Validate DTO inputs objects.
- Global YAML configuration.
- Custom Configuration via Input Attribute per controller action.

### Installation

[](#installation)

Require the bundle with composer:

```
composer require sfmok/request-input-bundle
```

### How to use

[](#how-to-use)

- Create DTO input and implements `Sfmok\RequestInput\InputInterface`

```
use Sfmok\RequestInput\InputInterface;

class PostInput implements InputInterface
{
    #[Assert\NotBlank]
    private string $title;

    #[Assert\NotBlank]
    private string $content;

    #[Assert\NotBlank]
    private array $tags;

    #[SerializedName('author')]
    #[Assert\NotBlank]
    private string $name;

    # getters and setters or make properties public
}
```

- Use DTO input in your controller action as an argument:

```
class PostController
{
    # Example with global config
    #[Route(path: '/posts', name: 'create')]
    public function create(PostInput $input): Response
    {
        dd($input);
    }

    # Example with specific config
    #[Route(path: '/posts', name: 'create')]
    #[Input(format: 'json', groups: ['create'], context: ['groups' => ['create']])]
    public function create(PostInput $input): Response
    {
        dd($input);
    }
}
```

### Validations

[](#validations)

- Response header

```
Content-Type: application/json; charset=utf-8

```

- Response body

```
{
  "type": "https://symfony.com/errors/validation",
  "title": "Validation Failed",
  "detail": "title: This value should not be blank.",
  "violations": [
    {
      "propertyPath": "title",
      "title": "This value should not be blank.",
      "parameters": {
        "{{ value }}": "\"\""
      },
      "type": "urn:uuid:c1051bb4-d103-4f74-8988-acbcafc7fdc3"
    }
  ]
}
```

### Deserialization

[](#deserialization)

Whether the request data contains invalid syntax or invalid attributes types a clear 400 json response will return:

- Data Error

```
{
  "title": 12
}
```

```
{
  "title": "Deserialization Failed",
  "detail": "Data error",
  "violations": [
    {
      "propertyPath": "title",
      "message": "This value should be of type string",
      "currentType": "int"
    }
  ]
}
```

- Syntax error:

```
{
  "title": "foo",
}
```

```
{
  "title": "Deserialization Failed",
  "detail": "Syntax error",
  "violations": []
}
```

### Configuration

[](#configuration)

```
# config/packages/request_input.yaml
request_input:
  enabled: true # default value true
  formats: ['json'] # default value ['json', 'xml', 'form']
  skip_validation: true # default value false
```

You can also override the format using attribute input and specify the format explicitly.

### Create DTO input outside controller

[](#create-dto-input-outside-controller)

You just need to inject `InputFactoryInterface` e.g:

```
