Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

FormComponent.php 1.8KB

pirms 3 gadiem
pirms 3 gadiem
pirms 3 gadiem
pirms 3 gadiem
pirms 3 gadiem
pirms 3 gadiem
pirms 3 gadiem
pirms 3 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. }