1 | <?php |
||
25 | class Renderer extends AbstractValidateRenderer |
||
26 | { |
||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $skipValidators = [ |
||
31 | 'Explode', |
||
32 | 'Upload' |
||
33 | ]; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $rules = []; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $messages = []; |
||
44 | |||
45 | /** |
||
46 | * @var RulePluginManager |
||
47 | */ |
||
48 | protected $rulePluginManager; |
||
49 | |||
50 | /** |
||
51 | * @param RulePluginManager $rulePluginManager |
||
52 | */ |
||
53 | public function setRulePluginManager(RulePluginManager $rulePluginManager) |
||
54 | { |
||
55 | $this->rulePluginManager = $rulePluginManager; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @return RulePluginManager |
||
60 | */ |
||
61 | public function getRulePluginManager() |
||
62 | { |
||
63 | return $this->rulePluginManager; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Executed before the ZF2 view helper renders the element |
||
68 | * |
||
69 | * @param string $formAlias |
||
70 | * @param \Zend\View\Renderer\PhpRenderer $view |
||
71 | * @param \Zend\Form\FormInterface $form |
||
72 | * @param array $options |
||
73 | * |
||
74 | * @return FormInterface |
||
75 | */ |
||
76 | public function preRenderForm($formAlias, View $view, FormInterface $form = null, array $options = []) |
||
77 | { |
||
78 | $form = parent::preRenderForm($formAlias, $view, $form, $options); |
||
79 | |||
80 | /** @var $options Options */ |
||
81 | $options = $this->getOptions(); |
||
82 | |||
83 | $inlineScript = $view->plugin('inlineScript'); |
||
84 | $inlineScript->appendScript($this->buildInlineJavascript($form, $options)); |
||
85 | |||
86 | if ($options->getIncludeAssets()) { |
||
87 | $assetBaseUri = $this->getHttpRouter()->assemble([], ['name' => 'strokerform-asset']); |
||
88 | $inlineScript->appendFile($assetBaseUri . '/jquery_validate/js/jquery.validate.js'); |
||
89 | $inlineScript->appendFile($assetBaseUri . '/jquery_validate/js/custom_rules.js'); |
||
90 | if ($options->isUseTwitterBootstrap() === true) { |
||
91 | $inlineScript->appendFile($assetBaseUri . '/jquery_validate/js/jquery.validate.bootstrap.js'); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | $this->reset(); |
||
96 | return $form; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @param \Zend\Form\FormInterface $form |
||
101 | * @param Options $options |
||
102 | * |
||
103 | * @return string |
||
104 | */ |
||
105 | protected function buildInlineJavascript(FormInterface $form, Options $options) |
||
106 | { |
||
107 | $validateOptions = []; |
||
108 | foreach ($options->getValidateOptions() as $key => $value) { |
||
109 | $value = (is_string($value)) ? $value : var_export($value, true); |
||
110 | $validateOptions[] = '"' . $key . '": ' . $value; |
||
111 | } |
||
112 | |||
113 | return sprintf( |
||
114 | $options->getInitializeTrigger(), |
||
115 | sprintf( |
||
116 | '$(\'form[name="%s"]\').each(function() { $(this).validate({%s"rules":%s,"messages":%s}); });', |
||
117 | $form->getName(), |
||
118 | count($validateOptions) > 0 ? implode(',', $validateOptions) . ',' : '', |
||
119 | Json::encode($this->rules), |
||
120 | Json::encode($this->messages) |
||
121 | ) |
||
122 | ); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @param string $formAlias |
||
127 | * @param \Zend\Form\ElementInterface $element |
||
128 | * @param \Zend\Validator\ValidatorInterface $validator |
||
129 | * |
||
130 | * @return mixed|void |
||
131 | */ |
||
132 | protected function addValidationAttributesForElement($formAlias, ElementInterface $element, ValidatorInterface $validator = null) |
||
133 | { |
||
134 | if (in_array($this->getValidatorClassName($validator), $this->skipValidators)) { |
||
135 | return; |
||
136 | } |
||
137 | if ($element instanceof Email && $validator instanceof Regex) { |
||
138 | $validator = new EmailAddress(); |
||
139 | } |
||
140 | |||
141 | $rule = $this->getRule($validator); |
||
142 | $rules = []; |
||
143 | if ($rule !== null) { |
||
144 | $rules = $rule->getRules($validator, $element); |
||
145 | $messages = $rule->getMessages($validator); |
||
146 | } elseif (!$this->getOptions()->isDisableAjaxFallback()) { |
||
|
|||
147 | //fallback ajax |
||
148 | $ajaxUri = $this->getHttpRouter()->assemble(['form' => $formAlias], ['name' => 'strokerform-ajax-validate']); |
||
149 | $rules = [ |
||
150 | 'remote' => [ |
||
151 | 'url' => $ajaxUri, |
||
152 | 'type' => 'POST' |
||
153 | ] |
||
154 | ]; |
||
155 | $messages = []; |
||
156 | } |
||
157 | |||
158 | if (count($rules) > 0) { |
||
159 | $elementName = $this->getElementName($element); |
||
160 | $this->addRules($elementName, $rules); |
||
161 | $this->addMessages($elementName, $messages); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * @param \Zend\Validator\ValidatorInterface $validator |
||
167 | * |
||
168 | * @return null|RuleInterface |
||
169 | */ |
||
170 | public function getRule(ValidatorInterface $validator = null) |
||
183 | |||
184 | /** |
||
185 | * @param string $elementName |
||
186 | * @param array $rules |
||
187 | */ |
||
188 | protected function addRules($elementName, array $rules = []) |
||
195 | |||
196 | /** |
||
197 | * @param string $elementName |
||
198 | * @param array $messages |
||
199 | */ |
||
200 | protected function addMessages($elementName, array $messages = []) |
||
207 | |||
208 | /** |
||
209 | * Resets previously set rules and messages, if you have multiple forms on one request |
||
210 | */ |
||
211 | protected function reset() |
||
216 | } |
||
217 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: