Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Created February 25, 2019 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurozumi/6ba276b05b0ea2653d5692b251b5e723 to your computer and use it in GitHub Desktop.
Save kurozumi/6ba276b05b0ea2653d5692b251b5e723 to your computer and use it in GitHub Desktop.
<?php
namespace App\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Doctrine\ORM\EntityManagerInterface;
use App\Service\FileUploader;
class ImageType extends AbstractType {
/**
* @var EntityManagerInterface
*/
protected $entityManager;
/**
* @var RequestStack
*/
protected $requestStack;
/**
* @var FileUploader
*/
protected $fileUploader;
public function __construct(
EntityManagerInterface $entityManager,
RequestStack $requestStack,
FileUploader $fileUploader
) {
$this->entityManager = $entityManager;
$this->requestStack = $requestStack;
$this->fileUploader = $fileUploader;
}
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('image', FileType::class, [
'required' => false,
'mapped' => false,
'constraints' => [
new Assert\File([
"mimeTypes" => ["image/jpg", "image/jpeg", "image/gif", "image/png"]
])
],
])
->add('file_name', HiddenType::class, [
'required' => false,
])
;
}
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults([
'data_class' => PresentMessage::class,
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment