1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the WPSymfonyForm plugin. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2015-2016 LIN3S <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace LIN3S\WPSymfonyForm\Controller; |
13
|
|
|
|
14
|
|
|
use LIN3S\WPSymfonyForm\Factory\FormFactory; |
15
|
|
|
use LIN3S\WPSymfonyForm\Translation\Translator; |
16
|
|
|
use LIN3S\WPSymfonyForm\Wrapper\FormWrapper; |
17
|
|
|
use Symfony\Component\Form\FormInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The controller that resolves AJAX call. |
21
|
|
|
* |
22
|
|
|
* @author Gorka Laucirica <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class AjaxController |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Manages ajax call, if success executes success actions found in formWrapper. |
28
|
|
|
* |
29
|
|
|
* @param FormWrapper $formWrapper The form wrapper |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public function ajaxAction(FormWrapper $formWrapper) |
34
|
|
|
{ |
35
|
|
|
$form = FormFactory::get()->create($formWrapper->getForm()); |
36
|
|
|
$form->handleRequest(); |
37
|
|
|
|
38
|
|
|
if ($form->isValid()) { |
39
|
|
|
foreach ($formWrapper->getSuccessActions() as $action) { |
40
|
|
|
$action->execute($form); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return json_encode([]); |
44
|
|
|
} else { |
45
|
|
|
http_response_code(400); |
46
|
|
|
|
47
|
|
|
return json_encode($this->getFormErrors($form)); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Returns serialized errors array. |
53
|
|
|
* |
54
|
|
|
* @param FormInterface $form The form |
55
|
|
|
* |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
|
|
protected function getFormErrors(FormInterface $form) |
59
|
|
|
{ |
60
|
|
|
$errors = []; |
61
|
|
|
foreach ($form->getErrors() as $error) { |
62
|
|
|
$errors[] = Translator::instance()->trans($error->getMessageTemplate(), $error->getMessageParameters(), 'validators'); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
foreach ($form->all() as $child) { |
65
|
|
|
if (!$child->isValid()) { |
66
|
|
|
$errors[$child->getName()] = $this->getFormErrors($child); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $errors; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.