-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathJsFormValidatorTwigExtension.php
More file actions
executable file
·91 lines (81 loc) · 2.09 KB
/
JsFormValidatorTwigExtension.php
File metadata and controls
executable file
·91 lines (81 loc) · 2.09 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
84
85
86
87
88
89
90
91
<?php
namespace Fp\JsFormValidatorBundle\Twig\Extension;
use Fp\JsFormValidatorBundle\Factory\JsFormValidatorFactory;
use Symfony\Component\Form\FormView;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
/**
* Class JsFormValidatorTwigExtension
*
* @package Fp\JsFormValidatorBundle\Twig\Extension
*/
class JsFormValidatorTwigExtension extends AbstractExtension
{
/**
* @var JsFormValidatorFactory
*/
protected $factory;
/**
* @return JsFormValidatorFactory
* @codeCoverageIgnore
*/
protected function getFactory()
{
return $this->factory;
}
/**
* @param JsFormValidatorFactory $factory
*
* @codeCoverageIgnore
*/
public function __construct(JsFormValidatorFactory $factory)
{
$this->factory = $factory;
}
/**
* {@inheritdoc}
*/
public function getFunctions()
{
return array(
new TwigFunction('init_js_validation', array($this, 'getJsValidator'), array(
'is_safe' => array('html')
)),
new TwigFunction('js_validator_config', array($this, 'getConfig'), array(
'is_safe' => array('html')
)),
);
}
public function getConfig()
{
return $this->getFactory()->getJsConfigString();
}
/**
* @param null|string|FormView $form
* @param bool $onLoad
* @param bool $wrapped
*
* @return string
*/
public function getJsValidator($form = null, $onLoad = true, $wrapped = true)
{
if ($form instanceof FormView) {
$form = $form->vars['name'];
}
$jsModels = $this->getFactory()->getJsValidatorString($form, $onLoad);
if ($wrapped) {
$jsModels = '<script type="text/javascript">' . $jsModels . '</script>';
}
return $jsModels;
}
/**
* Returns the name of the extension.
*
* @return string The extension name
* @codeCoverageIgnore
*/
public function getName()
{
return 'fp_js_form_validator';
}
}