8000 Expected argument of type "array or (\Traversable and \ArrayAccess)", "string" given · Issue #31041 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Expected argument of type "array or (\Traversable and \ArrayAccess)", "string" given #31041

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

Closed
mayro opened this issue Apr 9, 2019 · 4 comments

Comments

@mayro
Copy link
mayro commented Apr 9, 2019

Symfony version(s) affected: 3.4.24

Hello,
When I upgrade my project from 3.3 to 3.4.24, I have some errors with form field and transformer.
with symfony 3.3 I had this code :

      ` ->add('labels', TextType::class, ['property_path' => 'labels']);

`
With a viewTransformer to transform ( I receive an array and i register a string in my database)

  $builder->get('labels')->addViewTransformer(new TestLabelsTransformer());
    /**
     * @param mixed $value
     * @return array
     */
    public function reverseTransform($value)
    {
        if (is_array($value)) {
            return implode(',', $value);
        }
        return $value;
    }

With the 3.4.24, i have an invalid value. So I change my code to use a CollectionType instead of TextType and i add a transformer but I get this error : Expected argument of type "array or (\Traversable and \ArrayAccess)", "string" given when i try to use a ModelTransformer to convert an array to a string.
In my Form:

            ->add('labels', CollectionType::class, ['entry_type' => TextType::class, 'allow_add' => true, 'allow_delete' =>true])

$builder->get('labels')
            ->addModelTransformer(new CallbackTransformer(
                function ($labelsAsString) {
                    return explode(', ', $labelsAsString);
                },
                function ($labelsAsArray) {
                    return implode(', ', $labelsAsArray);
                }
            ))
        ;

The validator catch the exception Type before passing by the ModelTransformer. ( With a patch method)

 Symfony\Component\Form\Extension\Core\EventListener\ResizeFormListener

public function preSetData(FormEvent $event)
    {
        $form = $event->getForm();
        $data = $event->getData();
// with the post 
        if (null === $data) {
            $data = [];
        }
// with patch  $data = 'the string1,string2'  sor it doesn't use the ModelTransformer 
        if (!\is_array($data) && !($data instanceof \Traversable && $data instanceof \ArrayAccess)) {
            throw new UnexpectedTypeException($data, 'array or (\Traversable and \ArrayAccess)');
        }}

the same error here #30622
Thank you

@mayro
Copy link
Author
mayro commented Apr 10, 2019

Hello,

it's the same issue #29809. I fixed it by addiding a custom Type with
$resolver->setDefaults(array(
'compound' => false,
'multiple' => true,
));

Fixed by 000e4aa

Thank you

@Simperfit
Copy link
Contributor

@mayro does this mean you don't any problem anymore ?

@chalasr
Copy link
Member
chalasr commented Apr 10, 2019

Closing as fixed, feel free to reopen.

@chalasr chalasr closed this as completed Apr 10, 2019
@mayro
Copy link
Author
mayro commented Apr 10, 2019

@Simperfit Yes I fixed it with a customType with multiple option, and i added a ModelTransformer to convert the array to string.
It seems to work fine Thanks

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

No branches or pull requests

5 participants
0