Guest User

Untitled

a guest
Jun 24th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.43 KB | None | 0 0
  1. if ($request->getMethod() == 'POST') {
  2.  
  3. $form->bind($request);
  4.  
  5. if ($form->isValid()) {
  6.  
  7. $em->persist($team);
  8. $em->flush();
  9. ...
  10. }
  11. }
  12.  
  13. /**
  14. * @ORMPrePersist()
  15. * @ORMPreUpdate()
  16. */
  17. public function uploadImage() {
  18. // the file property can be empty if the field is not required
  19. if (null === $this->image) {
  20. return;
  21. }
  22. if(!$this->id){
  23. $this->image->move($this->getTmpUploadRootDir(), $this->image->getClientOriginalName());
  24. }else{
  25. $this->image->move($this->getUploadRootDir(), $this->image->getClientOriginalName());
  26. }
  27. $this->setImage($this->image->getClientOriginalName());
  28. }
  29.  
  30. $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
  31. // Retrieve submitted data
  32. $form = $event->getForm();
  33. $item = $event->getData();
  34.  
  35. // Test if upload image is null (maybe adapt it to work with your code)
  36. if (null !== $form->get('image')->getData()) {
  37. var_dump($form->get('image')->getData());
  38. die('image provided');
  39. $item->setImage($form->get('image')->getData());
  40. }
  41.  
  42. // src/Van/TeamsBundle/Form/TeamEditType.php
  43.  
  44. namespace VanTeamsBundleForm;
  45.  
  46. use SymfonyComponentFormFormBuilderInterface;
  47. use SymfonyComponentOptionsResolverOptionsResolverInterface;
  48. use SymfonyComponentFormFormEvent;
  49. use SymfonyComponentFormFormEvents;
  50.  
  51. class TeamEditType extends TeamType // Ici, on hérite de ArticleType
  52. {
  53. public function buildForm(FormBuilderInterface $builder, array $options)
  54. {
  55. // On fait appel à la méthode buildForm du parent, qui va ajouter tous les champs à $builder
  56. parent::buildForm($builder, $options);
  57. // On supprime celui qu'on ne veut pas dans le formulaire de modification
  58. $builder->remove('image')
  59. ->add('image', 'file', array(
  60. 'data_class' => null,
  61. 'required' => false
  62. ))
  63. ;
  64.  
  65.  
  66. $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
  67. // Retrieve submitted data
  68. $form = $event->getForm();
  69. $item = $event->getData();
  70.  
  71. // Test if upload image is null (maybe adapt it to work with your code)
  72. if (null !== $form->get('image')->getData()) {
  73. var_dump($form->get('image')->getData());
  74. die('image provided');
  75. $item->setImage($form->get('image')->getData());
  76. }
  77. });
  78.  
  79.  
  80. }
  81.  
  82. // On modifie cette méthode car les deux formulaires doivent avoir un nom différent
  83. public function getName()
  84. {
  85. return 'van_teamsbundle_teamedittype';
  86. }
  87. }
  88.  
  89. <?php
  90.  
  91. namespace VanTeamsBundleEntity;
  92.  
  93. use DoctrineORMMapping as ORM;
  94. use SymfonyComponentValidatorConstraints as Assert;
  95.  
  96. /**
  97. * Team
  98. *
  99. * @ORMTable()
  100. * @ORMHasLifecycleCallbacks
  101. * @ORMEntity
  102. * @ORMEntity(repositoryClass="VanTeamsBundleEntityTeamRepository") @ORMTable(name="van_teams")
  103. */
  104. class Team
  105. {
  106. /**
  107. * @var integer
  108. *
  109. * @ORMColumn(name="id", type="integer")
  110. * @ORMId
  111. * @ORMGeneratedValue(strategy="AUTO")
  112. */
  113. private $id;
  114.  
  115. /**
  116. * @var string
  117. *
  118. * @ORMColumn(name="name", type="string", length=100)
  119. */
  120. private $name;
  121.  
  122. /**
  123. * @var string
  124. *
  125. * @ORMColumn(name="countryCode", type="string", length=2)
  126. */
  127. private $countryCode;
  128.  
  129. /**
  130. * @ORMManyToOne(targetEntity="VanTeamsBundleEntityGame")
  131. * @ORMJoinColumn(nullable=false)
  132. */
  133. private $game;
  134.  
  135. /**
  136. * @ORMManyToOne(targetEntity="VanTeamsBundleEntityStatut")
  137. * @ORMJoinColumn(nullable=false)
  138. */
  139. private $statut;
  140.  
  141. /**
  142. * @var string $image
  143. * @AssertFile( maxSize = "1024k", mimeTypesMessage = "Please upload a valid Image")
  144. * @ORMColumn(name="image", type="string", length=255)
  145. */
  146. private $image;
  147.  
  148.  
  149.  
  150. /**
  151. * Get id
  152. *
  153. * @return integer
  154. */
  155. public function getId()
  156. {
  157. return $this->id;
  158. }
  159.  
  160. /**
  161. * Set name
  162. *
  163. * @param string $name
  164. * @return Team
  165. */
  166. public function setName($name)
  167. {
  168. $this->name = $name;
  169.  
  170. return $this;
  171. }
  172.  
  173. /**
  174. * Get name
  175. *
  176. * @return string
  177. */
  178. public function getName()
  179. {
  180. return $this->name;
  181. }
  182.  
  183. /**
  184. * Set countryCode
  185. *
  186. * @param string $countryCode
  187. * @return Team
  188. */
  189. public function setCountryCode($countryCode)
  190. {
  191. $this->countryCode = $countryCode;
  192.  
  193. return $this;
  194. }
  195.  
  196. /**
  197. * Get countryCode
  198. *
  199. * @return string
  200. */
  201. public function getCountryCode()
  202. {
  203. return $this->countryCode;
  204. }
  205.  
  206. /**
  207. * Set image
  208. *
  209. * @param string $image
  210. * @return Team
  211. */
  212. public function setImage($image)
  213. {
  214. $this->image = $image;
  215.  
  216. return $this;
  217. }
  218.  
  219. /**
  220. * Get image
  221. *
  222. * @return string
  223. */
  224. public function getImage()
  225. {
  226. return $this->image;
  227. }
  228.  
  229. /**
  230. * Set game
  231. *
  232. * @param VanTeamsBundleEntityGame $game
  233. * @return Team
  234. */
  235. public function setGame(VanTeamsBundleEntityGame $game)
  236. {
  237. $this->game = $game;
  238.  
  239. return $this;
  240. }
  241.  
  242. /**
  243. * Get game
  244. *
  245. * @return VanTeamsBundleEntityGame
  246. */
  247. public function getGame()
  248. {
  249. return $this->game;
  250. }
  251.  
  252. /**
  253. * Set statut
  254. *
  255. * @param VanTeamsBundleEntityStatut $statut
  256. * @return Team
  257. */
  258. public function setStatut(VanTeamsBundleEntityStatut $statut)
  259. {
  260. $this->statut = $statut;
  261.  
  262. return $this;
  263. }
  264.  
  265. /**
  266. * Get statut
  267. *
  268. * @return VanTeamsBundleEntityStatut
  269. */
  270. public function getStatut()
  271. {
  272. return $this->statut;
  273. }
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280. public function getFullImagePath() {
  281. return null === $this->image ? null : $this->getUploadRootDir(). $this->image;
  282. }
  283.  
  284. protected function getUploadRootDir() {
  285. // the absolute directory path where uploaded documents should be saved
  286. // return $this->getTmpUploadRootDir();
  287. return __DIR__ . '/../../../../web/uploads/';
  288. }
  289.  
  290. protected function getTmpUploadRootDir() {
  291. // the absolute directory path where uploaded documents should be saved
  292. return __DIR__ . '/../../../../web/uploads_tmp/';
  293. }
  294.  
  295. /**
  296. * @ORMPrePersist()
  297. * @ORMPreUpdate()
  298. */
  299. public function uploadImage() {
  300. // the file property can be empty if the field is not required
  301. if (null === $this->image) {
  302. return;
  303. }
  304. if(!$this->id){
  305. $this->image->move($this->getTmpUploadRootDir(), $this->image->getClientOriginalName());
  306. }else{
  307. $this->image->move($this->getUploadRootDir(), $this->image->getClientOriginalName());
  308. }
  309. $this->setImage($this->image->getClientOriginalName());
  310. }
  311.  
  312. /**
  313. * @ORMPostPersist()
  314. */
  315. public function moveImage()
  316. {
  317. if (null === $this->image) {
  318. return;
  319. }
  320. if(!is_dir($this->getUploadRootDir())){
  321. mkdir($this->getUploadRootDir());
  322. }
  323. copy($this->getTmpUploadRootDir().$this->image, $this->getFullImagePath());
  324. unlink($this->getTmpUploadRootDir().$this->image);
  325. }
  326.  
  327. /**
  328. * @ORMPreRemove()
  329. */
  330. public function removeImage()
  331. {
  332. unlink($this->getFullImagePath());
  333. rmdir($this->getUploadRootDir());
  334. }
  335. }
  336.  
  337. $builder->remove('image')
  338. ->add('image', 'file', array(
  339. 'data_class' => null,
  340. 'required' => false
  341. ))
  342. ;
  343.  
  344. $form = $this->createForm(FooType::class, $foo, array(
  345. 'action' => $this->generateUrl('foo_update', array('id' => $foo->getId())),
  346. 'method' => 'PATCH',
  347. ));
  348.  
  349. public function buildForm(FormBuilderInterface $builder, array $options)
  350. {
  351. // $builder->add() ...
  352.  
  353. $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormInterface $form) {
  354. // Retrieve submitted data
  355. $form = $event->getForm();
  356. $image = $form->getData();
  357.  
  358. // Test if upload image is null (maybe adapt it to work with your code)
  359. if (null !== $form->get('uploadImage')->getData()) {
  360. $image->setUploadImage($form->get('uploadImage')->getData());
  361. }
  362. });
  363. }
  364.  
  365. public function setImage($image)
  366. {
  367. if($image !== null) {
  368. $this->image = $image;
  369.  
  370. return $this;
  371. }
  372. }
  373.  
  374. /**
  375. * @var string
  376. *
  377. * @ORMColumn(name="image", type="string")
  378. *
  379. */
  380. private $image;
  381.  
  382. /**
  383. *
  384. * @AssertFile(mimeTypes={ "image/jpeg", "image/jpg", "image/png" })
  385. */
  386. private $file;
  387.  
  388. public function setFile($file)
  389. {
  390. $this->file = $file;
  391. return $this;
  392. }
  393. public function getFile()
  394. {
  395. return $this->file;
  396. }
  397.  
  398. // other code i.e image setter getter
  399. ...
  400.  
  401. $builder->add('file', FileType::class, array(
  402. 'data_class' => null,
  403. 'required'=>false,
  404. 'label' => 'Upload Image (jpg, jpeg, png file)')
  405. );
  406.  
  407. <div class="form-group">
  408. {{ form_label(form.file) }}
  409. {{ form_widget(form.file, {'attr': {'class': 'form-control'}}) }}
  410. </div>
  411.  
  412. ...
  413. if ($form->isSubmitted() && $form->isValid()) {
  414. $file = $item->getFile();
  415. if($file instanceof UploadedFile) {
  416. $fileName = md5(uniqid()).'.'.$file->guessExtension();
  417. $file->move(
  418. $this->getParameter('upload_directory'),
  419. $fileName
  420. );
  421. $item->setImage($fileName);
  422. }
  423. ...
  424. }
  425. ...
  426.  
  427. parameters:
  428. upload_directory: '%kernel.project_dir%/web/uploads'
Add Comment
Please, Sign In to add comment