1

请我需要你的帮助,我正在尝试使用 nelmio 包插入图像,但它给了我这个错误参数标签值''违反了约束(类型为 \"array 或 Traversable\",\"string\" 的预期参数)"

我的控制器如下

     /**
 * @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);
}

请问如何解决

4

1 回答 1

1

该错误来自您自己的要求,您需要nullable=falsemap=true您没有传递任何值(即value "" violated a constraint)。

在您的:中将nullable属性设置为 falseRequestParam

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

或为您的参数分配值tags

PS:问题是关于 FOSRestBundle,而不是 nelmio/api-doc-bundle,这里用于记录您的 api。

编辑

要在 nelmio 沙箱中插入数组,请将其用作键值:

Key: tags[] , Value: 'tag1'

并为您要传递的每个标签(一个新的键值对)执行此操作。

于 2016-04-11T15:26:03.220 回答