Expected argument of type "array or (\Traversable and \ArrayAccess) in Symfony collection field type

3.8k views Asked by At

I get the message :

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

during the execution of this code:

$form = $this->createFormBuilder([], ['csrf_protection' => false])
   ->add('collectionField', 'collection', [
       'type' => 'text', 
       'constraints' => [new Type('array')]]
    )
   ->add('integerField', 'integer', [
       'constraints' => [new Type('integer')]]
   )
   ->getForm();

$form->submit(['collectionField' => 'error', 'integerField' => 'error']);

This is because I pass string data to "collectionField" field.

But when I pass array to "integerField":

$form->submit(['collectionField' => [], 'integerField' => []]);

I get a normal message "This value is not valid.".

I think the behaviour should be the same. Of course, I could make some work around and find some solution but I'd be interested to know if it's a bug or if I missed something.

1

There are 1 answers

1
Sergii Shostak On BEST ANSWER

Ok. Answer on my question by myself. It's a bug and will be fixed in the nearest Symfony release. https://github.com/symfony/symfony/pull/13851