anony
anony

Reputation: 185

Parameter tags value '' violated a constraint (Expected argument of type \"array or Traversable\", \"string\" given)"

please i need your help, i'm trying to insert an image using nelmio bundle but it gives me this error Parameter tags value '' violated a constraint (Expected argument of type \"array or Traversable\", \"string\" given)"

my controller is as follows

     /**
 * @ApiDoc(description="Uploads photo with tags.")
 *
 * @Rest\FileParam(name="image", image=true, description="Image to upload.")
 * @Rest\RequestParam(name="tags", requirements=".+", nullable=false, map=true, description="Tags that associates photo.")
 * @Rest\View()
 */
public function postPhotoAction(ParamFetcher $paramFetcher, array $tags)
{
    $em = $this->getDoctrine()->getManager();

    $photo = new Photo();
    $form = $this->createForm(new PhotoType, $photo);

    if ($tags) {
        $tags = $em->getRepository('TestTaskTagsBundle:Tag')->findOrCreateByTitles($tags);
    }

    $form->submit($paramFetcher->all());

    if (!$form->isValid()) {
        return $form->getErrors();
    }

    foreach ($tags as $tag) {
        $photo->addTag($tag);
    }

    $em->persist($photo);
    $em->flush();

    return array('photo' => $photo);
}

how to solve that please

Upvotes: 1

Views: 863

Answers (1)

chalasr
chalasr

Reputation: 13167

The error come from your own requirements, you are requiring nullable=false and map=true but you don't pass any value (i.e. value "" violated a constraint).

Set the nullable attribute to false in your RequestParam:

* @Rest\RequestParam(name="tags", requirements=".+", nullable=true, map=true, description="Tags that associates photo.")

Or assign values to your tags parameter.

PS: The problem is about FOSRestBundle, not nelmio/api-doc-bundle that is here only for document your api.

EDIT

To insert an array in nelmio sandbox, use this as key value:

Key: tags[] , Value: 'tag1'

And do this for each tag you want to pass (a new key-value pair).

Upvotes: 1

Related Questions