public function renderJavascript(\Twig_Environment $twig, FormView $view) { if (!isset($view->vars['rule_context'])) { return ''; } /** @var \Boekkooi\Bundle\JqueryValidationBundle\Form\FormRuleContext $rootContext */ $template = 'BoekkooiJqueryValidationBundle:Form:form_validate.js.twig'; $rootContext = $context = $view->vars['rule_context']; $rootView = $view; // The given view is not the root form if ($view->parent !== null) { $template = 'BoekkooiJqueryValidationBundle:Form:dynamic_validate.js.twig'; $rootView = FormHelper::getViewRoot($view); $rootContext = $rootView->vars['rule_context']; } // Create template variables $templateVars = array('form' => $rootView, 'fields' => $this->fieldRulesViewData($context), 'validation_groups' => $this->validationGroupsViewData($rootContext)); $templateVars['enforce_validation_groups'] = count($templateVars['validation_groups']) > 1; $templateVars['enabled_validation_groups'] = count($rootContext->getButtons()) === 0 ? $templateVars['validation_groups'] : array(); // Only add buttons from the root form if ($view->parent === null) { $templateVars['buttons'] = $this->buttonsViewData($context); } $js = $twig->render($template, $templateVars); return preg_replace('/\\s+/', ' ', $js); }
public function buildView(FormView $view, FormInterface $form, array $options) { /** @var FormInterface | ClickableInterface $form */ if (!$form instanceof ClickableInterface) { return; } $viewRoot = FormHelper::getViewRoot($view); if (!$this->hasRuleBuilderContext($viewRoot)) { return; } /** @var FormRuleContextBuilder $context */ $context = $viewRoot->vars['rule_builder']; $context->addButton($view, FormHelper::getValidationGroups($form)); }
/** * @param FormView $view * @return FormRuleContextBuilder */ protected function getRuleBuilder(FormView $view) { $viewRoot = FormHelper::getViewRoot($view); if (!isset($viewRoot->vars['rule_builder'])) { throw new LogicException('getRuleBuilder is called before it was set by buildView'); } return $viewRoot->vars['rule_builder']; }