symfony2从表单中断言文件类型会引发意外的TypeException


symfony2 assert a file type from within a form throws unexpectedTypeException

使用symfony2/原则2,我希望使用验证约束对应用程序中上传的文件执行检查。

由于基础实体是一个通用媒体实体,可以用于整个网站的不同类型的媒体,我想将这些验证约束放在相关的表单类型中。

当我没有断言上传是正确的。当这样做时,我得到以下错误:

Expected argument of type "string", "Doctrine'ORM'PersistentCollection" given
Stack Trace
in vendor/symfony/symfony/src/Symfony/Component/Validator/Constraints/FileValidator.php at line 119   -
        }
        if (!is_scalar($value) && !$value instanceof FileObject && !(is_object($value) && method_exists($value, '__toString'))) {
            throw new UnexpectedTypeException($value, 'string');
        }
        $path = $value instanceof FileObject ? $value->getPathname() : (string) $value;

我的表单类型:

    ->add('medias', 'collection', array(
            'type' => new MediaType(),
            'by_reference' => false,
            'label'=>'Images et vidéos',
            'allow_add' => true,
            'allow_delete' => true,
            'required' => false,
            'cascade_validation' => true,
            'constraints' => array(
                new Assert'File(array('mimeTypes' => array("video/mpeg"), 'mimeTypesMessage' => "The file is not a video"))
            ),
            'attr'=>array(
                'data-toggle'=>"tooltip",
                'data-placement'=>"top",
                'title'=>"Ajoutez des images ou vidéos pour décrire la recette",
            )))

我做错了什么?

Arf,我累了,我应该把这个断言放到另一个assert''All中。我收到错误,因为它是一个集合。