-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSimpleFormProvider.php
More file actions
83 lines (74 loc) · 2.52 KB
/
SimpleFormProvider.php
File metadata and controls
83 lines (74 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/**
* @copyright 2017 Hostnet B.V.
*/
namespace Hostnet\Component\Form\Simple;
use Hostnet\Component\Form\Exception\FormNotFoundException;
use Hostnet\Component\Form\FormFailureHandlerInterface;
use Hostnet\Component\Form\FormHandlerInterface;
use Hostnet\Component\Form\FormProviderInterface;
use Hostnet\Component\Form\FormSuccessHandlerInterface;
use Hostnet\Component\Form\NamedFormHandlerInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* @author Iltar van der Berg <ivanderberg@hostnet.nl>
* @author Yannick de Lange <ydelange@hostnet.nl>
*
* @deprecated Use the HandlerFactoryInterface instead. Will be removed in version 2.0.
*/
class SimpleFormProvider implements FormProviderInterface
{
/**
* @var FormFactoryInterface
*/
private $form_factory;
/**
* @param FormFactoryInterface $form_factory
*/
public function __construct(FormFactoryInterface $form_factory)
{
$this->form_factory = $form_factory;
}
/**
* {@inheritdoc}
*/
public function handle(Request $request, FormHandlerInterface $handler, FormInterface $form = null)
{
if (null !== $form) {
$handler->setForm($form);
} elseif (null === ($form = $handler->getForm())) {
if ($handler instanceof NamedFormHandlerInterface && null !== ($name = $handler->getName())) {
$form = $this->form_factory->createNamed(
$name,
$handler->getType(),
$handler->getData(),
$handler->getOptions()
);
} else {
$form = $this->form_factory->create(
$handler->getType(),
$handler->getData(),
$handler->getOptions()
);
}
if (!$form instanceof FormInterface) {
throw new FormNotFoundException($handler);
}
// set the form which is associated with the handler
$handler->setForm($form);
}
$form->handleRequest($request);
if (!$form->isSubmitted()) {
return null;
}
if ($form->isValid()) {
if ($handler instanceof FormSuccessHandlerInterface) {
return $handler->onSuccess($request);
}
} elseif ($handler instanceof FormFailureHandlerInterface) {
return $handler->onFailure($request);
}
}
}