/** * @param Request $request * @param * $form * @param * $errors */ protected function saveErrorsToFlashBag(Request $request, Form $form) { $formClass = get_class($form->getConfig()->getType()->getInnerType()); $errors = []; foreach ($form->getErrors(TRUE) as $error) { $cause = $error->getCause(); $errors[] = ['message' => $error->getMessage(), 'messageTemplate' => $error->getMessageTemplate(), 'messageParameters' => $error->getMessageParameters(), 'messagePluralization' => $error->getMessagePluralization(), 'cause' => preg_replace('/data\\.|children\\[(.*?)\\]/', '$1', $cause->getPropertyPath()), 'invalidValue' => $cause->getInvalidValue()]; } $this->addFlash('validation_error_' . $formClass, $errors); $this->addFlash('validation_error_request_' . $formClass, $request); }
/** * @param Form $form * * @return array */ protected function getGroups($form) { $result = array(); $result['groups'] = $form->getConfig()->getOption('validation_groups'); $result['children'] = array(); /** @var Form $element */ foreach ($form->all() as $name => $element) { $result['children'][$name] = $this->getGroups($element); } return $result; }
/** * @param Form $form Main form. * @param string $providerName Provider. */ protected function buildTypeSettingsForm(Form $form, $providerName) { // create sub-form wrapper $subForm = $form->getConfig()->getFormFactory()->createNamed('properties', 'form', null, array('label' => false, 'auto_initialize' => false)); //Add provider form if ($providerName && isset($this->providers[$providerName])) { $provider = $this->providers[$providerName]; // delegate form structure building for specific provider $provider->buildForm($subForm); } $form->add($subForm); }
public function addField(Form $form, $parser = null) { if ($parser === null) { return false; } $options = $form->getConfig()->getOptions(); $em = $options['em']; if (!$parser instanceof Newscoop\IngestPluginBundle\Entity\Parser) { $parser = $em->getRepository('Newscoop\\IngestPluginBundle\\Entity\\Parser')->findOneById($parser); } if ($parser->requiresUrl()) { $form->add('url', 'url', array('label' => 'plugin.ingest.feeds.url', 'required' => true)); } }
/** * Converts list of constraints objects to a data array * * @param array $constraints * * @return array */ protected function parseConstraints(array $constraints) { $result = array(); foreach ($constraints as $item) { // Translate messages if need and add to result foreach ($item as $propName => $propValue) { if (false !== strpos(strtolower($propName), 'message')) { $item->{$propName} = $this->translateMessage($propValue); } } if ($item instanceof \Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity) { $item = new UniqueEntity($item, $this->currentElement->getConfig()->getDataClass()); } $result[get_class($item)][] = $item; } return $result; }
/** * Returns a string representation of all form errors (including children errors). * * This method should only be used in ajax calls. * * @param Form $form The form to parse * @param bool $withChildren Do we parse the embedded forms * * @return string A string representation of all errors */ public function getRecursiveReadableErrors($form, $withChildren = true, $translationDomain = null, $level = 0) { $errors = ''; $translationDomain = $translationDomain ? $translationDomain : $form->getConfig()->getOption('translation_domain'); //the errors of the fields foreach ($form->getErrors() as $error) { //the view contains the label identifier $view = $form->createView(); $labelId = $view->vars['label']; //get the translated label if ($labelId !== null) { $label = $this->translator->trans($labelId, [], $translationDomain) . ' : '; } else { $label = ''; } //in case of dev mode, we display the item that is a problem //getCause cames in Symfony 2.5 version, this is just a fallback to avoid BC with previous versions if ($this->debug && method_exists($error, 'getCause')) { $cause = $error->getCause(); if ($cause !== null) { $causePropertyPath = $cause->getPropertyPath(); $errors .= ' ' . $causePropertyPath; } } //add the error $errors .= $label . $this->translator->trans($error->getMessage(), [], $translationDomain) . "\n"; } //do we parse the children if ($withChildren) { //we parse the children foreach ($form->getIterator() as $child) { $level++; if ($err = $this->getRecursiveReadableErrors($child, $withChildren, $translationDomain, $level)) { $errors .= $err; } } } return $errors; }
/** * Get the conditon builder object for the given form. * * @param Form $form * @return ConditionBuilderInterface */ protected function getConditionBuilder(Form $form) { $builderClosure = $form->getConfig()->getAttribute('filter_condition_builder'); $builder = new ConditionBuilder(); if ($builderClosure instanceof \Closure) { $builderClosure($builder); } else { $this->buildDefaultConditionNode($form, $builder->root('AND')); } return $builder; }
private function getSessionName(Form $form) { return 'filter_form_' . $form->getConfig()->getName(); }
/** * @param Request $request * @param Form $form * @return $this */ public function load(Request $request, Form $form) { $this->_load($request, $form->getConfig()->getMethod(), $form->getName()); $form->handleRequest($request); return $this; }
/** * @param Form $form * @param array $metadata * @param string $prefix * * @return array|string */ protected function modelizeForm(Form $form, &$metadata, $prefix = null) { if ($form->getConfig()->getCompound()) { $all = $form->all(); $model = []; foreach ($all as $formField) { /** @var Form $formField */ $model[$formField->getName()] = $this->modelizeForm($formField, $metadata, null === $prefix ? '' : $prefix . ($prefix ? '.' : '') . $form->getName()); } ksort($model); return $model; } else { $value = 'string'; if ($form->getConfig()->getOption('choices')) { $value = array_keys($form->getConfig()->getOption('choices')); $metadata[$prefix . ($prefix ? '.' : '') . $form->getName()] = ['type' => lcfirst(preg_replace('/^api/', '', $form->getConfig()->getType()->getName())), 'constraints' => $this->buildConstraints($form) + [['type' => 'choices', 'vars' => ['values' => $value]]], 'default' => !is_object($form->getConfig()->getOption('empty_data')) ? $form->getConfig()->getOption('empty_data') : null]; } elseif ($form->getConfig()->getOption('type')) { /** @var AbstractType $type */ $type = $this->modelizeForm($form->getConfig()->getOption('type'), $metadata, ($prefix ? '.' : '') . $prefix); unset($type); $metadata[$prefix . ($prefix ? '.' : '') . $form->getName()] = ['type' => lcfirst(preg_replace('/^api/', '', $form->getConfig()->getType()->getName())), 'constraints' => $this->buildConstraints($form), 'default' => !is_object($form->getConfig()->getOption('empty_data')) ? $form->getConfig()->getOption('empty_data') : null]; $value = []; } else { $metadata[$prefix . ($prefix ? '.' : '') . $form->getName()] = ['type' => lcfirst(preg_replace('/^api/', '', $form->getConfig()->getType()->getName())), 'constraints' => $this->buildConstraints($form), 'default' => !is_object($form->getConfig()->getOption('empty_data')) ? $form->getConfig()->getOption('empty_data') : null]; } return $value; } }
/** * @param Form $form The form to inspect * @return string The resulting CSRF token */ private function getCsrfToken(Form $form) { /** @var \Symfony\Component\Form\FormConfigInterface $c */ $c = $form->getConfig(); /** @var \Symfony\Component\Security\CSRF\CsrfTokenManager $tokMgr */ $tokMgr = $c->getOption("csrf_token_manager"); /** @var \Symfony\Component\Security\CSRF\CsrfToken $csrf_token */ $token = $tokMgr->getToken('form'); return $token->getValue(); }
/** * @param Form $item * @param array $validationOutput * @param $camelCasedKey * * @return null|string */ private function getFormat(Form $item, array $validationOutput, $camelCasedKey) { $type = $item->getConfig()->getType()->getInnerType(); if (BooleanType::class === $type) { return '[false|true|0|1]'; } return isset($validationOutput[$camelCasedKey]['format']) ? $validationOutput[$camelCasedKey]['format'] : null; }