我有一个问题,即替换上传文件的形式错误。
实体:
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
/**
* Class Document
*
* @package Cfw\SiteBundle\Entity
* @ORM\Entity(repositoryClass="Cfw\SiteBundle\Entity\Repository\Document\DocumentRepository")
* @ORM\HasLifecycleCallbacks
* @ORM\Table
*/
class Document
{
use IdentityEntity;
use TimestampableEntity;
CONST UPLOAD_LIMIT = 10;
/**
* Document path
* @var string
*
* @ORM\Column(type="text", length=255, nullable=false)
*/
protected $path;
/**
* @var string
*
* @ORM\Column(type="text", length=50, nullable=false)
*/
protected $type;
/**
* @var string
*
* @ORM\Column(type="text", length=255, nullable=false)
*/
protected $name;
/**
* Image file
*
* @var File
*
*/
protected $file;
....
}
表格类型:
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DocumentType extends AbstractType
{
/**
* @var ContainerInterface $container
*/
protected $container;
/**
* DocumentType constructor.
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add(
'file',
null,
[
'required' => false,
'attr' => [
'label' => false,
'title' => 'form.document.browse',
],
'validation_groups' => ['Default', 'Document'],
]
);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(
[
'error_bubbling' => true,
'data_class' => 'Cfw\SiteBundle\Entity\Document\Document',
'translation_domain' => 'messages',
'cascade_validation' => true,
'validation_groups' => ['Default', 'Document'],
]
);
}
public function getName()
{
return 'cfw_document';
}
}
控制器:
public function documentAction(Request $request)
{
/**
* @var $user \Cfw\UserBundle\Entity\User
*/
$user = $this->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
$form = $this->createForm('cfw_document');
$form->handleRequest($request);
if ($form->isValid()) {
$this->get('cfw.document')->uploadDocument($form->getData(), $user);
}
return $this->render(
'CfwUserBundle:Profile:document.html.twig',
[
'form' => $form->createView(),
'documents' => $this->get('cfw.document')->getUserDocuments($user),
'upload_limit' => $this->get('cfw.document')->getUploadLimit(),
]
);
}
查看:
{{ form_start(form) }}
<div class="col-lg-8 document-form">
<div class="row form-message">
{{ form_errors(form) }}
</div>
<div class="row">
<h3>{{ 'form.document.title'|trans }}</h3>
</div>
<div class="row">
<div class="col-lg-12 col-md-12">
{{ 'form.document.description'|trans }}
</div>
</div>
<div class="row top10">
<div class="col-lg-6 col-md-6">
{{ form_widget(form.file) }}
</div>
<div class="col-lg-6 col-md-6">
{{ 'form.document.allowed_types'|trans }}<br />
<span class="allowed-size">{{ 'form.document.allowed_size'|trans }}</span>
</div>
</div>
<div class="row">
<div class="col-md-4 col-lg-4 submit-block">
<button type="submit" class="btn btn-warning btn-block">{{ 'form.document.submit'|trans }}</button>
</div>
</div>
</div>
{{ form_end(form) }}
验证配置为:
Cfw\SiteBundle\Entity\Document\Document:
properties:
file:
- File:
maxSize: "10M"
mimeTypes: ["image/jpeg", "image/gif", "image/png", "image/tiff", "application/pdf"]
maxSizeMessage: document.file.wrong_size
mimeTypesMessage: document.file.wrong_format
groups: [Default, Document]
麻烦的是,如果我上传zip文件需要显示错误类型的错误。但是,没有显示任何内容,也没有上传文件(当pdf或图像正常时)。
如果显示更改{{form_errors(form.file)}}错误,但我认为不正确。在字段文件中显示的分析器错误中,但$ form-&gt; getErrors()为空。
有人可以说出了什么问题吗?
答案 0 :(得分:2)
我在DocumentType中遇到错误。需要添加
'error_bubbling' => true
到字段“file”,而不是setDefaults()