1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Lug package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Lug\Component\Resource\Form\Type; |
13
|
|
|
|
14
|
|
|
use Lug\Component\Resource\Factory\FactoryInterface; |
15
|
|
|
use Lug\Component\Resource\Model\ResourceInterface; |
16
|
|
|
use Symfony\Component\Form\AbstractType; |
17
|
|
|
use Symfony\Component\Form\FormInterface; |
18
|
|
|
use Symfony\Component\OptionsResolver\Options; |
19
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
20
|
|
|
use Symfony\Component\Validator\Constraint; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author GeLo <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class ResourceType extends AbstractType |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
8 |
|
public function configureOptions(OptionsResolver $resolver) |
31
|
|
|
{ |
32
|
|
|
$resolver |
33
|
8 |
|
->setRequired(['resource', 'factory']) |
34
|
8 |
|
->setAllowedTypes('resource', ResourceInterface::class) |
35
|
8 |
|
->setAllowedTypes('factory', FactoryInterface::class) |
36
|
8 |
|
->setDefaults([ |
37
|
|
|
'data_class' => function (Options $options) { |
38
|
7 |
|
return $options['resource']->getModel(); |
39
|
8 |
|
}, |
40
|
|
|
'label_prefix' => function (Options $options) { |
41
|
6 |
|
return 'lug.'.$options['resource']->getName(); |
42
|
8 |
|
}, |
43
|
|
|
'validation_groups' => function (Options $options) { |
44
|
6 |
|
return [Constraint::DEFAULT_GROUP, 'lug.'.$options['resource']->getName()]; |
45
|
8 |
|
}, |
46
|
8 |
|
'empty_data' => function (FormInterface $form) { |
47
|
4 |
|
return $form->isRequired() || !$form->isEmpty() |
48
|
4 |
|
? $form->getConfig()->getOption('factory')->create() |
49
|
4 |
|
: null; |
50
|
8 |
|
}, |
51
|
8 |
|
]); |
52
|
8 |
|
} |
53
|
|
|
} |
54
|
|
|
|