1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Form; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Form\AbstractType; |
6
|
|
|
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; |
7
|
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
8
|
|
|
use Symfony\Component\Form\Extension\Core\Type\CollectionType; |
9
|
|
|
use Symfony\Component\Form\Extension\Core\Type\IntegerType; |
10
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
11
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
12
|
|
|
use Symfony\Component\Form\FormEvent; |
13
|
|
|
use Symfony\Component\Form\FormEvents; |
14
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
15
|
|
|
|
16
|
|
|
class QuestionType extends AbstractType |
17
|
|
|
{ |
18
|
2 |
|
public function buildForm(FormBuilderInterface $builder, array $options) |
19
|
|
|
{ |
20
|
2 |
|
$newSort = $options['new_sort']; |
21
|
|
|
$sortAttrs = [ |
22
|
|
|
'attr' => [ |
23
|
2 |
|
'class' => 'form-control', |
24
|
2 |
|
], |
25
|
2 |
|
'choices' => [1=>1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20], |
26
|
2 |
|
'choices_as_values' => true, |
27
|
2 |
|
]; |
28
|
|
|
|
29
|
2 |
|
if($newSort)$sortAttrs['data'] = $newSort; |
30
|
|
|
|
31
|
|
|
$builder |
32
|
2 |
|
->add('textQuestion', TextType::class, [ |
33
|
|
|
'attr' => [ |
34
|
2 |
|
'class' => 'form-control', |
35
|
|
|
'placeholder' => 'enter question' |
36
|
2 |
|
] |
37
|
2 |
|
]) |
38
|
2 |
|
->add('sort', ChoiceType::class, $sortAttrs) |
39
|
2 |
|
->add('allIncorrect', CheckboxType::class, [ |
40
|
2 |
|
'required' => false, |
41
|
|
|
'label' => 'Choose it if the question has no correct answers ' |
42
|
2 |
|
]) |
43
|
2 |
|
->add('answers', CollectionType::class, [ |
44
|
2 |
|
'entry_type' => AnswerType::class, |
45
|
2 |
|
'allow_add' => true, |
46
|
2 |
|
'allow_delete' => true, |
47
|
2 |
|
'by_reference' => false, |
48
|
|
|
'attr' => [ |
49
|
|
|
'class' => '' |
50
|
2 |
|
], |
51
|
|
|
|
52
|
2 |
|
]) |
53
|
2 |
|
->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) { |
54
|
|
|
$data = $event->getData(); |
55
|
|
|
|
56
|
|
|
foreach ($data->getAnswers() as $item) { |
57
|
|
|
$item->setQuestion($data); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$event->setData($data); |
61
|
2 |
|
}); |
62
|
|
|
|
63
|
|
|
|
64
|
2 |
|
} |
65
|
|
|
|
66
|
2 |
|
public function configureOptions(OptionsResolver $resolver) |
67
|
|
|
{ |
68
|
2 |
|
$resolver->setDefaults(array( |
69
|
2 |
|
'data_class' => 'AppBundle\Entity\Question', |
70
|
|
|
'new_sort' => '' |
71
|
2 |
|
)); |
72
|
2 |
|
} |
73
|
|
|
|
74
|
2 |
|
public function getBlockPrefix() |
75
|
|
|
{ |
76
|
2 |
|
return 'app_bundle_question_type'; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|