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); } }
private function addNumberCheck(FormView $view, RuleCollection $rules, RuleMessage $message = null, array $conditions = array()) { if (!$this->useGroupRule && count($conditions) > 0) { $rules->set(RequiredRule::RULE_NAME, new TransformerRule(RequiredRule::RULE_NAME, true, $message, $conditions)); } // Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToArrayTransformer switch ($view->vars['name']) { case 'year': $rules->set(NumberRule::RULE_NAME, new TransformerRule(NumberRule::RULE_NAME, true, $message, $conditions)); return; case 'month': $min = 1; $max = 12; break; case 'day': $min = 1; $max = 31; break; case 'hour': $min = 0; $max = 23; break; case 'minute': case 'second': $min = 0; $max = 59; break; default: return; } $rules->set(MinRule::RULE_NAME, new TransformerRule(MinRule::RULE_NAME, $min, $message, $conditions)); $rules->set(MaxRule::RULE_NAME, new TransformerRule(MaxRule::RULE_NAME, $max, $message, $conditions)); }
/** * {@inheritdoc} */ public function resolve(Constraint $constraint, FormInterface $form, RuleCollection $collection) { if (!$this->supports($constraint, $form)) { throw new LogicException(); } /** @var \Symfony\Component\Validator\Constraints\Luhn $constraint */ $collection->set(self::RULE_NAME, new ConstraintRule(self::RULE_NAME, true, new RuleMessage($constraint->message), $constraint->groups)); }
/** * {@inheritdoc} */ public function resolve(Constraint $constraint, FormInterface $form, RuleCollection $collection) { if (!$this->supports($constraint, $form)) { throw new LogicException(); } /** @var \Symfony\Component\Validator\Constraints\Choice|\Symfony\Component\Validator\Constraints\Length $constraint */ $collection->set(self::RULE_NAME, new ConstraintRule(self::RULE_NAME, $constraint->min, new RuleMessage($constraint->minMessage, array('{{ limit }}' => $constraint->min), (int) $constraint->min), $constraint->groups)); }
/** * {@inheritdoc} */ public function resolve(Constraint $constraint, FormInterface $form, RuleCollection $collection) { if (!$this->supports($constraint, $form)) { throw new LogicException(); } /** @var \Symfony\Component\Validator\Constraints\File $constraint */ $collection->set(self::RULE_NAME, new ConstraintRule(self::RULE_NAME, implode(',', $constraint->mimeTypes), new RuleMessage($constraint->mimeTypesMessage, array('{{ types }}' => implode(', ', $constraint->mimeTypes))), $constraint->groups)); }
private function processDateTime(FormView $view, FormConfigInterface $config, FormRuleContextBuilder $context) { if ($config->getOption('format') !== DateTimeType::HTML5_FORMAT) { return; } $rules = new RuleCollection(); $rules->set('date', new TransformerRule('date', true, $this->getFormRuleMessage($config))); $context->add($view, $rules); }
/** * {@inheritdoc} */ public function resolve(Constraint $constraint, FormInterface $form, RuleCollection $collection) { if (!$this->supports($constraint, $form)) { throw new LogicException(); } $message = null; if ($constraint instanceof Range) { $message = new RuleMessage($constraint->invalidMessage); } elseif ($constraint instanceof Type) { $message = new RuleMessage($constraint->message, array('{{ type }}' => $constraint->type)); } $collection->set(self::RULE_NAME, new ConstraintRule(self::RULE_NAME, true, $message, $constraint->groups)); }
/** * {@inheritdoc} */ public function resolve(Constraint $constraint, FormInterface $form, RuleCollection $collection) { /** @var \Symfony\Component\Validator\Constraints\Url $constraint */ if (!$this->supports($constraint, $form)) { throw new LogicException(); } // jquery validate only validates https, http, ftp // So don't add the rule if there is some other protocol $diff = array_diff($constraint->protocols, array('http', 'https', 'ftp')); if (!empty($diff)) { return; } $collection->set(self::RULE_NAME, new ConstraintRule(self::RULE_NAME, true, new RuleMessage($constraint->message), $constraint->groups)); }
/** * {@inheritdoc} * @param */ public function resolve(Constraint $constraint, FormInterface $form, RuleCollection $collection) { $constraintClass = get_class($constraint); /** @var \Symfony\Component\Validator\Constraints\AbstractComparison $constraint */ if ($constraintClass === 'Symfony\\Component\\Validator\\Constraints\\GreaterThan') { $rule = new ConstraintRule(self::RULE_NAME, $constraint->value + 1, new RuleMessage($constraint->message, array('{{ compared_value }}' => $constraint->value)), $constraint->groups); } elseif ($constraintClass === 'Symfony\\Component\\Validator\\Constraints\\GreaterThanOrEqual') { $rule = new ConstraintRule(self::RULE_NAME, $constraint->value, new RuleMessage($constraint->message, array('{{ compared_value }}' => $constraint->value)), $constraint->groups); } elseif ($constraintClass === 'Symfony\\Component\\Validator\\Constraints\\Range' && $constraint->min !== null) { $rule = new ConstraintRule(self::RULE_NAME, $constraint->min, new RuleMessage($constraint->minMessage, array('{{ limit }}' => $constraint->min)), $constraint->groups); } else { throw new LogicException(); } $collection->set(self::RULE_NAME, $rule); }
/** * {@inheritdoc} */ public function resolve(Constraint $constraint, FormInterface $form, RuleCollection $collection) { if (!$this->supports($constraint, $form)) { throw new LogicException(); } $boolType = $constraint instanceof IsTrue; // If the isTrue is applied and it is a checkbox then just make it required if ($boolType && $form->getConfig()->getType()->getInnerType() instanceof CheckboxType) { $collection->set(RequiredRule::RULE_NAME, new ConstraintRule(RequiredRule::RULE_NAME, true, new RuleMessage($constraint->message), $constraint->groups)); return; } // A additional method is used to skip if not found if (!$this->useAdditional) { return; } /** @var \Symfony\Component\Validator\Constraints\IsTrue | \Symfony\Component\Validator\Constraints\IsFalse $constraint */ $collection->set('equals', new ConstraintRule('equals', $boolType ? '1' : '0', new RuleMessage($constraint->message), $constraint->groups)); }
/** * {@inheritdoc} */ public function resolve(Constraint $constraint, FormInterface $form, RuleCollection $collection) { if (!$this->supports($constraint, $form)) { throw new LogicException(); } /** @var \Symfony\Component\Validator\Constraints\Ip $constraint */ $ruleOptions = true; switch ($constraint->version) { case Ip::V4: case Ip::V4_NO_PRIV: case Ip::V4_NO_RES: case Ip::V4_ONLY_PUBLIC: if (!$this->useIpv4) { return; } $ruleName = self::RULE_NAME_V4; break; case Ip::V6: case Ip::V6_NO_PRIV: case Ip::V6_NO_RES: case Ip::V6_ONLY_PUBLIC: if (!$this->useIpv6) { return; } $ruleName = self::RULE_NAME_V6; break; case Ip::ALL: case Ip::ALL_NO_PRIV: case Ip::ALL_NO_RES: case Ip::ALL_ONLY_PUBLIC: if (!$this->useOrRule || !$this->useIpv6 || !$this->useIpv4) { return; } $ruleName = self::RULE_NAME_OR; $ruleOptions = array(self::RULE_NAME_V4 => true, self::RULE_NAME_V6 => true); break; default: return; } $collection->set('ip', new ConstraintRule($ruleName, $ruleOptions, new RuleMessage($constraint->message), $constraint->groups)); }