<?php
declare(strict_types=1);
namespace App\Form\EventListener\Poll;
use App\Entity\Poll\Question;
use App\Entity\Poll\QuestionOption;
use App\Entity\Poll\ResponseAnswer;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
/**
* @author Przemysław Chrupek <przemyslaw.chrupek@avt.pl>
* @package App\Form\EventListener
*/
class ResponseResponseAnswerFormListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [FormEvents::PRE_SET_DATA => 'preSetData'];
}
public function preSetData(FormEvent $event)
{
$responseAnswer = $event->getData();
if (!$responseAnswer instanceof ResponseAnswer)
return;
$question = $responseAnswer->getQuestion();
if (!$question instanceof Question)
return;
$form = $event->getForm();
$fieldOptions = [
'label' => $question->getTItle(),
'required' => false,
];
switch ($question->getType()) {
case Question::TYPE_SELECT:
$fieldOptions['class'] = QuestionOption::class;
$fieldOptions['choices'] = $question->getQuestionOptions();
$fieldOptions['choice_label'] = 'title';
$fieldOptions['placeholder'] = $question->getPlaceholder();
$fieldOptions['attr']['class'] = 'select-js';
if ($question->getRequired()) {
$fieldOptions['required'] = true;
$fieldOptions['constraints'] = [
new NotBlank([
'message' => 'Pole wymagane',
]),
];
}
break;
case Question::TYPE_MULTISELECT:
$fieldOptions['class'] = QuestionOption::class;
$fieldOptions['choices'] = $question->getQuestionOptions();
$fieldOptions['choice_label'] = 'title';
$fieldOptions['placeholder'] = $question->getPlaceholder();
$fieldOptions['multiple'] = true;
$fieldOptions['attr']['class'] = 'multiselect-js';
if ($question->getRequired()) {
$fieldOptions['required'] = true;
$fieldOptions['constraints'] = [
new NotBlank([
'message' => 'Pole wymagane',
]),
];
}
break;
case Question::TYPE_CHECKBOX:
$fieldOptions['class'] = QuestionOption::class;
$fieldOptions['choices'] = $question->getQuestionOptions();
$fieldOptions['choice_label'] = 'title';
$fieldOptions['expanded'] = true;
$fieldOptions['multiple'] = true;
if ($question->getRequired()) {
$fieldOptions['required'] = true;
$fieldOptions['constraints'] = [
new NotBlank([
'message' => 'Pole wymagane',
]),
];
}
break;
case Question::TYPE_RADIO:
$fieldOptions['class'] = QuestionOption::class;
$fieldOptions['choices'] = $question->getQuestionOptions();
$fieldOptions['choice_label'] = 'title';
$fieldOptions['expanded'] = true;
$fieldOptions['multiple'] = false;
$fieldOptions['placeholder'] = false;
if ($question->getRequired()) {
$fieldOptions['required'] = true;
$fieldOptions['constraints'] = [
new NotBlank([
'message' => 'Pole wymagane',
]),
];
}
break;
case Question::TYPE_INPUT_NUMBER:
$fieldOptions['placeholder'] = $question->getPlaceholder();
$fieldOptions['html5'] = true;
$fieldOptions['input'] = 'number';
if ($question->getRequired()) {
$fieldOptions['required'] = true;
$fieldOptions['constraints'] = [
new NotBlank([
'message' => 'Pole wymagane'
]),
];
}
break;
case Question::TYPE_INPUT_TIME:
$fieldOptions['placeholder'] = $question->getPlaceholder();
$fieldOptions['input_format'] = 'HH:mm:ss';
if ($question->getRequired()) {
$fieldOptions['required'] = true;
$fieldOptions['constraints'] = [
new NotBlank([
'message' => 'Pole wymagane'
]),
];
}
break;
case Question::TYPE_INPUT_DATE:
$fieldOptions['placeholder'] = $question->getPlaceholder();
$fieldOptions['format'] = 'dd.MM.yyyy';
$fieldOptions['attr']['class'] = 'date-js';
$fieldOptions['input'] = 'string';
if ($question->getRequired()) {
$fieldOptions['required'] = true;
$fieldOptions['constraints'] = [
new NotBlank([
'message' => 'Pole wymagane'
]),
];
}
break;
case Question::TYPE_INPUT_DATETIME:
$fieldOptions['placeholder'] = $question->getPlaceholder();
$fieldOptions['format'] = 'dd.MM.yyyy HH:mm:ss';
$fieldOptions['attr']['class'] = 'datetime-js';
$fieldOptions['input'] = 'string';
if ($question->getRequired()) {
$fieldOptions['required'] = true;
$fieldOptions['constraints'] = [
new NotBlank([
'message' => 'Pole wymagane'
]),
];
}
break;
case Question::TYPE_INPUT_TEXT:
if ($question->getRequired()) {
$fieldOptions['required'] = true;
$fieldOptions['constraints'] = [
new NotBlank([
'message' => 'Pole wymagane'
]),
];
}
break;
}
$fieldName = $question->isSelect() ? 'valueOption' : 'value';
$form->add(
$fieldName,
$question->getFieldFormTypeClass(),
$fieldOptions
);
}
}