Symfony Forms TsvTransformer
Install the gupalo/symfony-form-transformers package using composer:
composer require gupalo/symfony-form-transformersEmptyStringTransformer: 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)
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);
}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;