Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

arrays in Symfony Forms #74

Open
tacman opened this issue Sep 20, 2020 · 4 comments
Open

arrays in Symfony Forms #74

tacman opened this issue Sep 20, 2020 · 4 comments

Comments

@tacman
Copy link
Contributor

tacman commented Sep 20, 2020

I'm trying to implement a data transformer to save integer[] values in a postgres database using the bundle.

// some entity with tags
/**
 * @ORM\Column(type="integer[]", nullable=true)
 */
    protected $tagIds;

    public function getTagIds(): array
    {
        return $this->tagIds ?: [];
    }

    public function setTagIds($tagIds): self
    {
        $this->tagIds = $tagIds;
        return $this;
    }

I'd like to add these tags Id via a custom form type (for now, just a string of comma-delimited integers, for this issue report)

        $builder
            ->add('tagIds', TagIdsType::class, [
                'required' => false,
                'help' => 'comma-delimited tag ids',
            ])
        ;

The TagIdsType, following the instructions from https://symfony.com/doc/current/form/create_custom_field_type.html

class TagIdsType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('tag_string', TextareaType::class, [
                'help' => 'comma-delimited tag ids',
            ])
        ;
        $builder->get('tag_string')->addModelTransformer(new TagIdsTransformer());

Finally, the TagIdsTransformer:

// TagIdsTransformer.php
namespace App\Form\DataTransformer;

use App\Entity\TaggableInterface;
use Symfony\Component\Form\DataTransformerInterface;

class TagIdsTransformer implements DataTransformerInterface

{
    public function transform($taggableEntity)
    {
        /** @var TaggableInterface $taggableEntity */
        return $taggableEntity ? implode(',', $taggableEntity->getTagIds()): '';
    }

    public function reverseTransform($tagsAsString)
    {
        dump($tagsAsString);
        $tagsAsArray =  array_map(fn ($idAsString) => (int)$idAsString, explode(',', $tagsAsString));
        dump($tagsAsArray);
        return $tagsAsArray;
    }
}

When I fill out the form with a string of comma-delimited integers, it fails with the message

One or more of items given doesn't look like valid. {"tag_string":[3]} array

The transform and reverseTransform appear to be returning the right values, so I'm not sure where the error is. Obviously, I don't want tag_string, it's not part of the entity.

I'm posting here because if someone provides a solution, I can submit a PR to the documentation, as I imagine this can help others as well. Thanks.

@martin-georgiev
Copy link
Owner

Did you find a solution to your use-case? Can this issue be closed?

@tacman
Copy link
Contributor Author

tacman commented Jan 15, 2023

I didn't find a solution, but I'm likely to try again this week.

@trading-developer
Copy link

I didn't find a solution, but I'm likely to try again this week.

Did you find a solution to your use-case? Can this issue be closed?

@fstronin
Copy link

fstronin commented Sep 7, 2023

@tacman , hi! Try to grep the validator component for the string "One or more of items given doesn't look like valid" because it seems that there is some validator that can not process the input data correctly. Have you assigned any constraints to your form?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants