Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/solidworx/formhandlerbundle

Easy form handling for Symfony forms
https://github.com/solidworx/formhandlerbundle

form-handler symfony symfony-bundle symfony-forms

Last synced: 3 months ago
JSON representation

Easy form handling for Symfony forms

Awesome Lists containing this project

README

        

# FormHandlerBundle

[![Build Status](https://travis-ci.org/SolidWorx/FormHandlerBundle.svg)](https://travis-ci.org/SolidWorx/FormHandlerBundle)

The FormHandler component attempts to make controllers with basic form handlers cleaner by off-loading form handling to separate classes.

# Table of Contents
- [Requirements](#requirements)
- [Installation](#installation)
- [Composer](#composer)
- [Usage](#usage)
- [Testing](#testing)
- [Contributing](#contributing)
- [Licence](#licence)

## Requirements

FormHandler requires PHP 7.1+ and Symfony 3.0+

## Installation

### Composer

```bash
$ composer require solidworx/form-handler-bundle:^1.0
```

Then register the bundle in your Symfony application:

```php
create(MyFormType::class);
}
}
```

The benefit of using the factory, is when you need to pass additional information or options to the form, E.G

```php
return $factory->create(MyFormType::class, null, ['horizontal' => true]);
```

To register your form handler, register it as a service:

```yaml
services:
my.form.handler:
class: MyFormHandler
tags:
- { name: 'form.handler' }
```

Inside your controller, use the `form.handler` service to handle your form:

```php
get('solidworx.form_handler')->handle(MyFormHandler::class); // MyFormHandler will automatically be pulled from the container if it is tagges with `form.handler`
}
}
```

This will process the necessary logic on the form (submit the form and handle the request etc).

If you need to handle a failed form, you need to implement the `FormHandlerFailInterface` interface:

```php
get('solidworx.form_handler')->handle(MyFormHandler::class, ['entity' => new Blog]); // MyFormHandler will automatically be pulled from the container if it is tagges with `form.handler`
}
}
```

The options will then be available in the `getForm` method as a `Options` object:

```php
create(MyFormType::class, $options->get('entity'));
}
}
```

You can also configure the options to set what options is allowed, set default values, define required options etc. by implementing the `FormHandlerOptionsResolver` interface:

```php
setRequired('entity');
$resolver->addAllowedTypes('entity', BlogEntity::class);
$resolver->setDefault('another_options', 'myvalue');
}
}
```

## Advanced Usage

That is the very basics of the component. There are more advanced usages where you can customize the handling of a form to your specific needs.

## Testing

To run the unit tests, execute the following command

```bash
$ vendor/bin/phpunit
```

## Contributing

See [CONTRIBUTING](https://github.com/SolidWorx/FormHandlerBundle/blob/master/CONTRIBUTING.md)

## License

FormHandler is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)

Please see the [LICENSE](LICENSE) file for the full license.