Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if ($request->getMethod() == 'POST') {
- $form->bind($request);
- if ($form->isValid()) {
- $em->persist($team);
- $em->flush();
- ...
- }
- }
- /**
- * @ORMPrePersist()
- * @ORMPreUpdate()
- */
- public function uploadImage() {
- // the file property can be empty if the field is not required
- if (null === $this->image) {
- return;
- }
- if(!$this->id){
- $this->image->move($this->getTmpUploadRootDir(), $this->image->getClientOriginalName());
- }else{
- $this->image->move($this->getUploadRootDir(), $this->image->getClientOriginalName());
- }
- $this->setImage($this->image->getClientOriginalName());
- }
- $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
- // Retrieve submitted data
- $form = $event->getForm();
- $item = $event->getData();
- // Test if upload image is null (maybe adapt it to work with your code)
- if (null !== $form->get('image')->getData()) {
- var_dump($form->get('image')->getData());
- die('image provided');
- $item->setImage($form->get('image')->getData());
- }
- // src/Van/TeamsBundle/Form/TeamEditType.php
- namespace VanTeamsBundleForm;
- use SymfonyComponentFormFormBuilderInterface;
- use SymfonyComponentOptionsResolverOptionsResolverInterface;
- use SymfonyComponentFormFormEvent;
- use SymfonyComponentFormFormEvents;
- class TeamEditType extends TeamType // Ici, on hérite de ArticleType
- {
- public function buildForm(FormBuilderInterface $builder, array $options)
- {
- // On fait appel à la méthode buildForm du parent, qui va ajouter tous les champs à $builder
- parent::buildForm($builder, $options);
- // On supprime celui qu'on ne veut pas dans le formulaire de modification
- $builder->remove('image')
- ->add('image', 'file', array(
- 'data_class' => null,
- 'required' => false
- ))
- ;
- $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
- // Retrieve submitted data
- $form = $event->getForm();
- $item = $event->getData();
- // Test if upload image is null (maybe adapt it to work with your code)
- if (null !== $form->get('image')->getData()) {
- var_dump($form->get('image')->getData());
- die('image provided');
- $item->setImage($form->get('image')->getData());
- }
- });
- }
- // On modifie cette méthode car les deux formulaires doivent avoir un nom différent
- public function getName()
- {
- return 'van_teamsbundle_teamedittype';
- }
- }
- <?php
- namespace VanTeamsBundleEntity;
- use DoctrineORMMapping as ORM;
- use SymfonyComponentValidatorConstraints as Assert;
- /**
- * Team
- *
- * @ORMTable()
- * @ORMHasLifecycleCallbacks
- * @ORMEntity
- * @ORMEntity(repositoryClass="VanTeamsBundleEntityTeamRepository") @ORMTable(name="van_teams")
- */
- class Team
- {
- /**
- * @var integer
- *
- * @ORMColumn(name="id", type="integer")
- * @ORMId
- * @ORMGeneratedValue(strategy="AUTO")
- */
- private $id;
- /**
- * @var string
- *
- * @ORMColumn(name="name", type="string", length=100)
- */
- private $name;
- /**
- * @var string
- *
- * @ORMColumn(name="countryCode", type="string", length=2)
- */
- private $countryCode;
- /**
- * @ORMManyToOne(targetEntity="VanTeamsBundleEntityGame")
- * @ORMJoinColumn(nullable=false)
- */
- private $game;
- /**
- * @ORMManyToOne(targetEntity="VanTeamsBundleEntityStatut")
- * @ORMJoinColumn(nullable=false)
- */
- private $statut;
- /**
- * @var string $image
- * @AssertFile( maxSize = "1024k", mimeTypesMessage = "Please upload a valid Image")
- * @ORMColumn(name="image", type="string", length=255)
- */
- private $image;
- /**
- * Get id
- *
- * @return integer
- */
- public function getId()
- {
- return $this->id;
- }
- /**
- * Set name
- *
- * @param string $name
- * @return Team
- */
- public function setName($name)
- {
- $this->name = $name;
- return $this;
- }
- /**
- * Get name
- *
- * @return string
- */
- public function getName()
- {
- return $this->name;
- }
- /**
- * Set countryCode
- *
- * @param string $countryCode
- * @return Team
- */
- public function setCountryCode($countryCode)
- {
- $this->countryCode = $countryCode;
- return $this;
- }
- /**
- * Get countryCode
- *
- * @return string
- */
- public function getCountryCode()
- {
- return $this->countryCode;
- }
- /**
- * Set image
- *
- * @param string $image
- * @return Team
- */
- public function setImage($image)
- {
- $this->image = $image;
- return $this;
- }
- /**
- * Get image
- *
- * @return string
- */
- public function getImage()
- {
- return $this->image;
- }
- /**
- * Set game
- *
- * @param VanTeamsBundleEntityGame $game
- * @return Team
- */
- public function setGame(VanTeamsBundleEntityGame $game)
- {
- $this->game = $game;
- return $this;
- }
- /**
- * Get game
- *
- * @return VanTeamsBundleEntityGame
- */
- public function getGame()
- {
- return $this->game;
- }
- /**
- * Set statut
- *
- * @param VanTeamsBundleEntityStatut $statut
- * @return Team
- */
- public function setStatut(VanTeamsBundleEntityStatut $statut)
- {
- $this->statut = $statut;
- return $this;
- }
- /**
- * Get statut
- *
- * @return VanTeamsBundleEntityStatut
- */
- public function getStatut()
- {
- return $this->statut;
- }
- public function getFullImagePath() {
- return null === $this->image ? null : $this->getUploadRootDir(). $this->image;
- }
- protected function getUploadRootDir() {
- // the absolute directory path where uploaded documents should be saved
- // return $this->getTmpUploadRootDir();
- return __DIR__ . '/../../../../web/uploads/';
- }
- protected function getTmpUploadRootDir() {
- // the absolute directory path where uploaded documents should be saved
- return __DIR__ . '/../../../../web/uploads_tmp/';
- }
- /**
- * @ORMPrePersist()
- * @ORMPreUpdate()
- */
- public function uploadImage() {
- // the file property can be empty if the field is not required
- if (null === $this->image) {
- return;
- }
- if(!$this->id){
- $this->image->move($this->getTmpUploadRootDir(), $this->image->getClientOriginalName());
- }else{
- $this->image->move($this->getUploadRootDir(), $this->image->getClientOriginalName());
- }
- $this->setImage($this->image->getClientOriginalName());
- }
- /**
- * @ORMPostPersist()
- */
- public function moveImage()
- {
- if (null === $this->image) {
- return;
- }
- if(!is_dir($this->getUploadRootDir())){
- mkdir($this->getUploadRootDir());
- }
- copy($this->getTmpUploadRootDir().$this->image, $this->getFullImagePath());
- unlink($this->getTmpUploadRootDir().$this->image);
- }
- /**
- * @ORMPreRemove()
- */
- public function removeImage()
- {
- unlink($this->getFullImagePath());
- rmdir($this->getUploadRootDir());
- }
- }
- $builder->remove('image')
- ->add('image', 'file', array(
- 'data_class' => null,
- 'required' => false
- ))
- ;
- $form = $this->createForm(FooType::class, $foo, array(
- 'action' => $this->generateUrl('foo_update', array('id' => $foo->getId())),
- 'method' => 'PATCH',
- ));
- public function buildForm(FormBuilderInterface $builder, array $options)
- {
- // $builder->add() ...
- $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormInterface $form) {
- // Retrieve submitted data
- $form = $event->getForm();
- $image = $form->getData();
- // Test if upload image is null (maybe adapt it to work with your code)
- if (null !== $form->get('uploadImage')->getData()) {
- $image->setUploadImage($form->get('uploadImage')->getData());
- }
- });
- }
- public function setImage($image)
- {
- if($image !== null) {
- $this->image = $image;
- return $this;
- }
- }
- /**
- * @var string
- *
- * @ORMColumn(name="image", type="string")
- *
- */
- private $image;
- /**
- *
- * @AssertFile(mimeTypes={ "image/jpeg", "image/jpg", "image/png" })
- */
- private $file;
- public function setFile($file)
- {
- $this->file = $file;
- return $this;
- }
- public function getFile()
- {
- return $this->file;
- }
- // other code i.e image setter getter
- ...
- $builder->add('file', FileType::class, array(
- 'data_class' => null,
- 'required'=>false,
- 'label' => 'Upload Image (jpg, jpeg, png file)')
- );
- <div class="form-group">
- {{ form_label(form.file) }}
- {{ form_widget(form.file, {'attr': {'class': 'form-control'}}) }}
- </div>
- ...
- if ($form->isSubmitted() && $form->isValid()) {
- $file = $item->getFile();
- if($file instanceof UploadedFile) {
- $fileName = md5(uniqid()).'.'.$file->guessExtension();
- $file->move(
- $this->getParameter('upload_directory'),
- $fileName
- );
- $item->setImage($fileName);
- }
- ...
- }
- ...
- parameters:
- upload_directory: '%kernel.project_dir%/web/uploads'
Add Comment
Please, Sign In to add comment