I have a promblem with displaing errors of form that upload file.
Entity:
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;
....
}
Form Type:
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';
}
}
Controller:
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(),
]
);
}
View:
{{ 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) }}
Validation config is:
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]
Trouble is if I upload zip file need to display error of wrong type. But, nothing is displaying, also file is not uploaded (when is pdf or image is ok).
if change in view to {{ form_errors(form.file) }} errors are displayed, but I think is not correct. In profiler error shown on field file, but $form->getErrors() is empty.
Can somebody say what is wrong?