/** * @param Form $form * @return array */ public static function getFormAsArray(Form $form) { $formTypesData = array(); $formTypes = $form->getIterator(); foreach ($formTypes as $formType) { /** @var Form $formType */ $formTypesData[$formType->getName()] = $formType->getData(); } return $formTypesData; }
public function formEncode($entity, Form $form, AbstractType $formType) { $baseName = $formType->getName(); $payload = array(); foreach ($form->getIterator() as $el) { if (is_array($entity)) { $payload[$baseName . '[' . $el->getName() . ']'] = $entity[$el->getName()]; } } return $payload; }
private function serialize(\Symfony\Component\Form\Form $form) { $local_errors = array(); foreach ($form->getIterator() as $key => $child) { foreach ($child->getErrors() as $error) { $local_errors[$key] = $error->getMessage(); } if (count($child->getIterator()) > 0) { $local_errors[$key] = $this->serialize($child); } } return $local_errors; }
public function bindForm(Form $form) { $fields = $form->getIterator(); /** @var \Symfony\Component\Form\Form $field */ foreach ($fields as $field) { $functionName = sprintf("set%s", Container::camelize($field->getName())); if (method_exists($this, $functionName)) { $this->{$functionName}($field->getData()); } else { $this->{$field->getName()} = $field->getData(); } } }
/** * 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 all form errors * @param Form $form * @return array */ private function getErrors(Form $form) { $errors = array(); $children = $form->getIterator(); /** @var Form $child */ foreach ($children as $child) { if (!$child->isValid()) { $errors[$form->getName() . '[' . $child->getName() . ']'] = $this->getErrors($child); } } foreach ($form->getErrors() as $key => $error) { $errors[] = $error->getMessage(); } return $errors; }
/** * Dado un formulario se devuelven sus errores parseados * @param Form $form * @param bool $deep option for Form getErrors method * @param bool $flatten option for Form getErrors method * @return array */ public function getFormErrors(Form $form, $deep = false, $flatten = true) { // Se parsean los errores que existan en el formulario para devolverlos en el reponse $errors = array(); //Se parsean los posibles errores generales del formulario(incluyendo los asserts a nivel de entidad) foreach ($form->getErrors($deep, $flatten) as $key => $error) { if ($form->isRoot()) { $errors['form'][] = $error->getMessage(); } else { $errors[] = $error->getMessage(); } } $childs = $form->getIterator(); //Se parsean los posibles errores de cada campo del formulario /** @var Form $child */ foreach ($childs as $child) { $fieldErrors = $child->getErrors(); while ($fieldErrors->current() != null) { $errors[$child->getName()][] = $fieldErrors->current()->getMessage(); $fieldErrors->next(); } } return $errors; }