PHPackages                             somnambulist/form-request-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. somnambulist/form-request-bundle

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

somnambulist/form-request-bundle
================================

Adds Laravel like form requests for Symfony; based on adamsafr/form-request-bundle

2.1.3(1y ago)33.5k12MITPHPPHP &gt;=8.1

Since Feb 6Pushed 1y ago3 watchersCompare

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

READMEChangelogDependencies (9)Versions (23)Used By (2)

Somnambulist Form Request Bundle
================================

[](#somnambulist-form-request-bundle)

[![GitHub Actions Build Status](https://camo.githubusercontent.com/502c278c523b734e51ad6a6084db1a0a377afee42c8e069a5b557d8b8e458e8a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f736f6d6e616d62756c6973742d746563682f666f726d2d726571756573742d62756e646c652f74657374732e796d6c3f6c6f676f3d676974687562266272616e63683d6d61696e)](https://github.com/somnambulist-tech/form-request-bundle/actions?query=workflow%3Atests)[![Issues](https://camo.githubusercontent.com/d48683f9e2aa028470dced5e643abcd8a01fc8b9dcfca22fd20ea3d3f6141307/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f736f6d6e616d62756c6973742d746563682f666f726d2d726571756573742d62756e646c653f6c6f676f3d676974687562)](https://github.com/somnambulist-tech/form-request-bundle/issues)[![License](https://camo.githubusercontent.com/e1b9b68122a7902141c7dcfbe34909afe5afbba7ddcbbc0ff7de998c4ec8924e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f736f6d6e616d62756c6973742d746563682f666f726d2d726571756573742d62756e646c653f6c6f676f3d676974687562)](https://github.com/somnambulist-tech/form-request-bundle/blob/master/LICENSE)[![PHP Version](https://camo.githubusercontent.com/73b5ce80fdb9af21815f2c2c77a0f6b13c012537f6525649a588582e4b956b63/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f736f6d6e616d62756c6973742f666f726d2d726571756573742d62756e646c653f6c6f676f3d706870266c6f676f436f6c6f723d7768697465)](https://packagist.org/packages/somnambulist/form-request-bundle)[![Current Version](https://camo.githubusercontent.com/a87c9d09db4f03a51583641ead20d97a5074459b2fb457718542207828f167b7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736f6d6e616d62756c6973742f666f726d2d726571756573742d62756e646c653f6c6f676f3d7061636b6167697374266c6f676f436f6c6f723d7768697465)](https://packagist.org/packages/somnambulist/form-request-bundle)

An implementation of form requests from Laravel for Symfony based on the original work by [Adam Sapraliev](https://github.com/adamsafr/form-request-bundle).

Requirements
------------

[](#requirements)

- PHP 8.0+
- symfony/framework-bundle

Installation
------------

[](#installation)

Install using composer, or checkout / pull the files from github.com.

- composer require somnambulist/form-request-bundle

Usage
-----

[](#usage)

Add the `SomnambulistFormRequestBundle` to your `bundles.php` list and add a config file in `packages`if you wish to configure the bundle. The following options can be set:

```
somnambulist_form_request:
    subscribers:
        authorization: true
        form_validation: true
```

`authorization` registers an event subscriber that will convert `AccessDeniedExceptions` to a JSON response. `form_validation` registers an event subscriber that will convert `FormValidationException` to a JSON response including the fields that failed validation and the rules and message that failed.

**Note:** the subscribers are enabled by default.

### Custom Rules

[](#custom-rules)

This package includes overrides for the following rules:

- required - validates a field is required supporting Symfony UploadedFile files
- uploaded\_file - validates an uploaded file supporting Symfony UploadedFile files
- mimes - validates an uploaded file mime-type is one of the given extensions

### Property Pass-Through

[](#property-pass-through)

The following `ParameterBag`s can be accessed via property accessors or method calls:

- attributes
- cookies
- files
- headers
- query
- request
- server

In addition, the following properties are also available:

- content
- session

### Making a Form Request

[](#making-a-form-request)

To make a form request, extend the base `Somnambulist\Bundles\FormRequestBundle\Http\FormRequest` class and override the `rules()` method to add your own validation rules. The `rules` method has access to the current request via the `source` property so rules can be constructed using request information.

The validation rules use [somnambulist/validation](https://github.com/somnambulist-tech/validation) instead of Symfony's validation component to allow for easier setup and extension.

For example: to make a form request specifically for validating the data to make a new user you could create the following:

```
