選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

61 行
1.8KB

  1. <?php
  2. namespace Lc\SovBundle\Component;
  3. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  4. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  5. use Symfony\Component\Form\FormBuilderInterface;
  6. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  7. use Symfony\Component\Validator\Constraints\EqualTo;
  8. use Symfony\Component\Validator\Constraints\NotNull;
  9. class FormComponent
  10. {
  11. protected ParameterBagInterface $parameterBag;
  12. public function __construct(ParameterBagInterface $parameterBag)
  13. {
  14. $this->parameterBag = $parameterBag;
  15. }
  16. public function addCaptchaType(FormBuilderInterface $builder)
  17. {
  18. $builder->add('specialField', HiddenType::class, [
  19. 'data' => 0,
  20. 'mapped' => false,
  21. 'attr' => [
  22. 'class' => 'special-field'
  23. ],
  24. 'constraints' => [
  25. new NotNull(),
  26. new EqualTo(['value' => $this->parameterBag->get('app.captcha_value'), 'message' => 'Valeur incorrecte'])
  27. ],
  28. ]);
  29. }
  30. // uploadImageTicketMessage
  31. public function uploadFile($form, $child, $directory, $subdirectory): ?string
  32. {
  33. $file = $form->get($child)->getData();
  34. if ($file) {
  35. $originalFilename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
  36. $newFilename = uniqid().'.'.$file->guessExtension();
  37. try {
  38. $file->move(
  39. $directory,
  40. $newFilename
  41. );
  42. } catch (FileException $e) {
  43. throw new \ErrorException("Une erreur est survenue lors de l'upload du fichier.");
  44. }
  45. return $subdirectory.$newFilename;
  46. }
  47. return false;
  48. }
  49. }