gupalo / symfony-form-transformers
Symfony Form Transformers
Installs: 11 720
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/gupalo/symfony-form-transformers
Requires
- php: >=8.4
- symfony/form: ^7.4|^8.0
- symfony/yaml: ^7.4|^8.0
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11.5
README
Symfony Forms TsvTransformer
How to Install
Install the gupalo/symfony-form-transformers package using composer:
composer require gupalo/symfony-form-transformers
Transformers
EmptyStringTransformer: allows user to pass empty fields which should be empty strings instead of nullJsonYamlTransformer: user views and edits field as YAML but it is stored as JSON in DB and is array in PHPStringArrayTransformer: user can enter several strings each from new line and they will become an arrayTsvTransformer: user copy-pastes from spreadsheet and PHP gets array with named keys (multiline and quoting are not supported)
Basic Usage
Add transformers to your forms
class YourEntity { private ?array $data = []; } /// class YourEntityType extends AbstractType { public function __construct(private JsonYamlTransformer $jsonYamlTransformer) { $this->jsonYamlTransformer = $jsonYamlTransformer; } public function buildForm(FormBuilderInterface $builder, array $options): void { $builder // ... ->add('data', TextareaType::class); $builder->get('data')->addModelTransformer($this->jsonYamlTransformer); }
Advanced Usage
Some transformers have options on construct. Options are public properties, you can change them later.
See tests for more examples. Also look at src - the logic is quite simple.
If you have multiline input Tsv, set TsvHelper::$multiline = true;