You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.7KB

  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. class FormComponent
  8. {
  9. protected ParameterBagInterface $parameterBag;
  10. public function __construct(ParameterBagInterface $parameterBag)
  11. {
  12. $this->parameterBag = $parameterBag;
  13. }
  14. public function addCaptchaType(FormBuilderInterface $builder)
  15. {
  16. $builder->add('specialField', HiddenType::class, [
  17. 'data' => 0,
  18. 'mapped' => false,
  19. 'attr' => [
  20. 'class' => 'special-field'
  21. ],
  22. 'constraints' => [
  23. new NotNull(),
  24. new EqualTo(['value' => $this->parameterBag->get('app.captcha_value'), 'message' => 'Valeur incorrecte'])
  25. ],
  26. ]);
  27. }
  28. // uploadImageTicketMessage
  29. public function uploadFile($form, $child, $directory, $subdirectory)
  30. {
  31. $file = $form->get($child)->getData();
  32. if ($file) {
  33. $originalFilename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
  34. $newFilename = uniqid().'.'.$file->guessExtension();
  35. try {
  36. $file->move(
  37. $directory,
  38. $newFilename
  39. );
  40. } catch (FileException $e) {
  41. throw new \ErrorException("Une erreur est survenue lors de l'upload du fichier.");
  42. }
  43. return $subdirectory.$newFilename;
  44. }
  45. return false;
  46. }
  47. }