public function process(FormRuleProcessorContext $context, FormRuleContextBuilder $collection) { $form = $context->getForm(); $formConfig = $form->getConfig(); if (!$formConfig->getCompound()) { return; } $transformer = $this->findTransformer($formConfig, 'Symfony\\Component\\Form\\Extension\\Core\\DataTransformer\\ValueToDuplicatesTransformer'); if ($transformer === null) { return; } $keys = $this->getKeys($transformer); if (empty($keys)) { return; } $formView = $context->getView(); // Use children here since we need the full_name $primary = array_shift($keys); $primaryView = $formView->children[$primary]; // Copy all rules to the first child/key element $ruleCollection = $collection->get($formView); if (!empty($ruleCollection)) { $collection->add($primaryView, $ruleCollection); } $collection->remove($formView); // Get correct error message if one is set. $invalidMessage = $this->getFormRuleMessage($formConfig); // Create equalTo rules for all other fields $equalToPrimaryRule = new TransformerRule('equalTo', FormHelper::generateCssSelector($primaryView), $invalidMessage, array(new FieldDependency($primaryView))); foreach ($keys as $childName) { $childCollection = new RuleCollection(); $childCollection->set('equalTo', $equalToPrimaryRule); $collection->add($formView->children[$childName], $childCollection); } }
public function process(FormRuleProcessorContext $context, FormRuleContextBuilder $formRuleContext) { $form = $context->getForm(); $formConfig = $form->getConfig(); if (!$formConfig->getCompound()) { return; } /** @var \Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToArrayTransformer $transformer */ $transformer = $this->findTransformer($formConfig, 'Symfony\\Component\\Form\\Extension\\Core\\DataTransformer\\DateTimeToArrayTransformer'); if ($transformer === null) { return; } $view = $context->getView(); $fields = $this->getTransformerFields($transformer); $invalidMessage = $this->getFormRuleMessage($formConfig); $views = array(); $conditions = array(); foreach ($fields as $fieldName) { $childView = $view->children[$fieldName]; // Get child rules collection $childRules = $formRuleContext->get($childView); if ($childRules === null) { $formRuleContext->add($childView, new RuleCollection()); $childRules = $formRuleContext->get($childView); } // Register rules $this->addNumberCheck($childView, $childRules, $invalidMessage, $conditions); $views[] = FormHelper::getFormName($childView); $conditions[] = new FieldDependency($childView); } if ($this->useGroupRule && count($views) > 1) { $rules = $formRuleContext->get(array_shift($views)); $rules->set(CompoundCopyToChildPass::RULE_NAME_GROUP_REQUIRED, new TransformerRule(CompoundCopyToChildPass::RULE_NAME_GROUP_REQUIRED, $views, $invalidMessage)); } }
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)); }
protected function extractRules(FormRuleContextBuilder $formRuleContext, FormInterface $form, FormView $view) { $extracted = new FormRuleContextBuilder(); if ($form->getConfig()->getCompound()) { $it = new \RecursiveIteratorIterator(new FormViewRecursiveIterator($view->getIterator()), \RecursiveIteratorIterator::SELF_FIRST); $found = array(); foreach ($it as $childView) { $found[] = FormHelper::getFormName($childView); } } else { $found = array($view); } foreach ($found as $foundView) { $rules = $formRuleContext->get($foundView); if ($rules === null) { continue; } $extracted->add($foundView, $rules); $formRuleContext->remove($foundView); } return $extracted; }
protected function isType(FormInterface $type, $typeName) { return FormHelper::isType($type->getConfig()->getType(), $typeName); }
/** * @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']; }
public function addGroup($view, $groups) { $groups = $this->normalizeGroups($groups); $name = FormHelper::getFormName($view); $this->groups[$name] = $groups; }
public function __construct($field, $condition = self::VALUE_EQUAL, $value) { $this->field = FormHelper::getFormName($field); $this->condition = $condition; $this->value = $value; }