src/Form/EventListener/Poll/ResponseResponseAnswerFormListener.php line 27

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Form\EventListener\Poll;
  4. use App\Entity\Poll\Question;
  5. use App\Entity\Poll\QuestionOption;
  6. use App\Entity\Poll\ResponseAnswer;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Form\FormEvent;
  9. use Symfony\Component\Form\FormEvents;
  10. use Symfony\Component\Validator\Constraints\Length;
  11. use Symfony\Component\Validator\Constraints\NotBlank;
  12. /**
  13.  * @author  Przemysław Chrupek <przemyslaw.chrupek@avt.pl>
  14.  * @package App\Form\EventListener
  15.  */
  16. class ResponseResponseAnswerFormListener implements EventSubscriberInterface
  17. {
  18.     public static function getSubscribedEvents()
  19.     {
  20.         return [FormEvents::PRE_SET_DATA => 'preSetData'];
  21.     }
  22.     public function preSetData(FormEvent $event)
  23.     {
  24.         $responseAnswer $event->getData();
  25.         if (!$responseAnswer instanceof ResponseAnswer)
  26.             return;
  27.         $question $responseAnswer->getQuestion();
  28.         if (!$question instanceof Question)
  29.             return;
  30.         $form $event->getForm();
  31.         $fieldOptions = [
  32.             'label'     => $question->getTItle(),
  33.             'required'  => false,
  34.         ];
  35.         switch ($question->getType()) {
  36.             case Question::TYPE_SELECT:
  37.                 $fieldOptions['class'] = QuestionOption::class;
  38.                 $fieldOptions['choices'] = $question->getQuestionOptions();
  39.                 $fieldOptions['choice_label'] = 'title';
  40.                 $fieldOptions['placeholder'] = $question->getPlaceholder();
  41.                 $fieldOptions['attr']['class'] = 'select-js';
  42.                 if ($question->getRequired()) {
  43.                     $fieldOptions['required'] = true;
  44.                     $fieldOptions['constraints'] = [
  45.                         new NotBlank([
  46.                             'message' => 'Pole wymagane',
  47.                         ]),
  48.                     ];
  49.                 }
  50.                 break;
  51.             case Question::TYPE_MULTISELECT:
  52.                 $fieldOptions['class'] = QuestionOption::class;
  53.                 $fieldOptions['choices'] = $question->getQuestionOptions();
  54.                 $fieldOptions['choice_label'] = 'title';
  55.                 $fieldOptions['placeholder'] = $question->getPlaceholder();
  56.                 $fieldOptions['multiple'] = true;
  57.                 $fieldOptions['attr']['class'] = 'multiselect-js';
  58.                 if ($question->getRequired()) {
  59.                     $fieldOptions['required'] = true;
  60.                     $fieldOptions['constraints'] = [
  61.                         new NotBlank([
  62.                             'message' => 'Pole wymagane',
  63.                         ]),
  64.                     ];
  65.                 }
  66.                 break;
  67.             case Question::TYPE_CHECKBOX:
  68.                 $fieldOptions['class'] = QuestionOption::class;
  69.                 $fieldOptions['choices'] = $question->getQuestionOptions();
  70.                 $fieldOptions['choice_label'] = 'title';
  71.                 $fieldOptions['expanded'] = true;
  72.                 $fieldOptions['multiple'] = true;
  73.                 if ($question->getRequired()) {
  74.                     $fieldOptions['required'] = true;
  75.                     $fieldOptions['constraints'] = [
  76.                         new NotBlank([
  77.                             'message' => 'Pole wymagane',
  78.                         ]),
  79.                     ];
  80.                 }
  81.                 break;
  82.             case Question::TYPE_RADIO:
  83.                 $fieldOptions['class'] = QuestionOption::class;
  84.                 $fieldOptions['choices'] = $question->getQuestionOptions();
  85.                 $fieldOptions['choice_label'] = 'title';
  86.                 $fieldOptions['expanded'] = true;
  87.                 $fieldOptions['multiple'] = false;
  88.                 $fieldOptions['placeholder'] = false;
  89.                 if ($question->getRequired()) {
  90.                     $fieldOptions['required'] = true;
  91.                     $fieldOptions['constraints'] = [
  92.                         new NotBlank([
  93.                             'message' => 'Pole wymagane',
  94.                         ]),
  95.                     ];
  96.                 }
  97.             break;
  98.             
  99.             case Question::TYPE_INPUT_NUMBER:
  100.                 $fieldOptions['placeholder'] = $question->getPlaceholder();
  101.                 $fieldOptions['html5'] = true;
  102.                 $fieldOptions['input'] = 'number';
  103.                 if ($question->getRequired()) {
  104.                     $fieldOptions['required'] = true;
  105.                     $fieldOptions['constraints'] = [
  106.                         new NotBlank([
  107.                             'message' => 'Pole wymagane'
  108.                         ]),
  109.                     ];
  110.                 }
  111.                 break;
  112.             case Question::TYPE_INPUT_TIME:
  113.                 $fieldOptions['placeholder'] = $question->getPlaceholder();
  114.                 $fieldOptions['input_format'] = 'HH:mm:ss';
  115.                 if ($question->getRequired()) {
  116.                     $fieldOptions['required'] = true;
  117.                     $fieldOptions['constraints'] = [
  118.                         new NotBlank([
  119.                             'message' => 'Pole wymagane'
  120.                         ]),
  121.                     ];
  122.                 }
  123.                 break;
  124.             case Question::TYPE_INPUT_DATE:
  125.                 $fieldOptions['placeholder'] = $question->getPlaceholder();
  126.                 $fieldOptions['format'] = 'dd.MM.yyyy';
  127.                 $fieldOptions['attr']['class'] = 'date-js';
  128.                 $fieldOptions['input'] = 'string';
  129.                 if ($question->getRequired()) {
  130.                     $fieldOptions['required'] = true;
  131.                     $fieldOptions['constraints'] = [
  132.                         new NotBlank([
  133.                             'message' => 'Pole wymagane'
  134.                         ]),
  135.                     ];
  136.                 }
  137.                 break;
  138.             case Question::TYPE_INPUT_DATETIME:
  139.                 $fieldOptions['placeholder'] = $question->getPlaceholder();
  140.                 $fieldOptions['format'] = 'dd.MM.yyyy HH:mm:ss';
  141.                 $fieldOptions['attr']['class'] = 'datetime-js';
  142.                 $fieldOptions['input'] = 'string';
  143.                 if ($question->getRequired()) {
  144.                     $fieldOptions['required'] = true;
  145.                     $fieldOptions['constraints'] = [
  146.                         new NotBlank([
  147.                             'message' => 'Pole wymagane'
  148.                         ]),
  149.                     ];
  150.                 }
  151.                 break;
  152.             case Question::TYPE_INPUT_TEXT:
  153.                 if ($question->getRequired()) {
  154.                     $fieldOptions['required'] = true;
  155.                     $fieldOptions['constraints'] = [
  156.                         new NotBlank([
  157.                             'message' => 'Pole wymagane'
  158.                         ]),
  159.                     ];
  160.                 }
  161.                 break;
  162.         }
  163.         $fieldName $question->isSelect() ? 'valueOption' 'value';
  164.         
  165.         $form->add(
  166.             $fieldName,
  167.             $question->getFieldFormTypeClass(),
  168.             $fieldOptions
  169.         );
  170.     }
  171. }