56 lines
1.7 KiB
PHP
56 lines
1.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Mummert\SurveyBundle\QuestionType;
|
|
|
|
use Mummert\SurveyBundle\Model\SurveyContentModel;
|
|
use Symfony\Component\Form\Extension\Core\Type\RangeType;
|
|
use Symfony\Component\Form\FormBuilderInterface;
|
|
use Symfony\Component\Validator\Constraints\NotBlank;
|
|
use Symfony\Component\Validator\Constraints\Range;
|
|
|
|
final class RangeQuestionType implements QuestionTypeInterface
|
|
{
|
|
public function getName(): string
|
|
{
|
|
return 'range';
|
|
}
|
|
|
|
public function getConstraints(SurveyContentModel $question): array
|
|
{
|
|
$constraints = [
|
|
new Range([
|
|
'min' => (int) $question->rangeMin,
|
|
'max' => (int) $question->rangeMax,
|
|
]),
|
|
];
|
|
|
|
if ('1' === (string) $question->mandatory) {
|
|
$constraints[] = new NotBlank();
|
|
}
|
|
|
|
return $constraints;
|
|
}
|
|
|
|
public function buildField(FormBuilderInterface $builder, SurveyContentModel $question, string $fieldName = 'answer'): void
|
|
{
|
|
$builder->add($fieldName, RangeType::class, [
|
|
'label' => false,
|
|
'constraints' => $this->getConstraints($question),
|
|
'attr' => [
|
|
'min' => (int) $question->rangeMin,
|
|
'max' => (int) $question->rangeMax,
|
|
'step' => max(1, (int) $question->rangeStep),
|
|
'class' => 'range range-primary w-full',
|
|
'data-controller' => 'range-preview',
|
|
'data-action' => 'input->range-preview#update',
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function normalizeAnswer(mixed $value, SurveyContentModel $question): string
|
|
{
|
|
return (string) (int) $value;
|
|
}
|
|
} |