Flatten form errors

@param FormInterface $form
@param boolean       $subForm

@return array
425540
public function getFlattenErrors(FormInterface $form = null, $subForm = false)
    {
        $form    = $form ?: $this->form;
        $flatten = [];

        foreach ($form->getErrors() as $error) {
            if ($subForm) {
                $flatten[] = $error->getMessage();
            } else {
                $path = $error->getCause()->getPropertyPath();

                if (!array_key_exists($path, $flatten)) {
                    $flatten[$path] = [$error->getMessage()];
                    continue;
                }

                $flatten[$path][] = $error->getMessage();
            }
        }

        $subForm = true;
        foreach ($form->all() as $key => $child) {
            $childErrors = $this->getFlattenErrors($child, $subForm);

            if (!empty($childErrors)) {
                $flatten[$key] = $childErrors;
            }
        }

        return $flatten;
    }
432120
protected function getAllErrors(FormInterface $form)
    {
        $errors = $form->getErrors();
        $children = $form->all();
        if (!count($errors) && !count($children)) {
            return null;
        }

        $section = [
            'field' => $form->getName()
        ];

        if (count($errors)) {
            $section['errors'] = array_map(
                function (FormError $error) {
                    return [
                        'message' => $error->getMessage(),
                        'parameters' => $error->getMessageParameters()
                    ];
                },
                iterator_to_array($form->getErrors())
            );
        }

        if (count($children)) {
            $section['children'] = array_values(
                array_filter(
                    array_map(
                        function (FormInterface $field) {
                            return $this->getAllErrors($field);
                        },
                        $form->all()
                    )
                )
            );

            if (!count($errors) && !count($section['children'])) {
                return null;
            }
        }

        return $section;
    }
602629
public function getSubmittedName(FormInterface $form)
    {
        foreach ($form as $field) {
            if ($field instanceof ClickableInterface && $field->isClicked()) {
                return $field->getName();
            }
        }

        return null;
    }
344259
private function appendProperForm(FormInterface $form, Definition $definition)
    {
        $identifier = $definition->getIdentifier();
        if ($form->has($identifier)) {
            return;
        }

        $type = sprintf('ekyna_%s_characteristic', $definition->getType());
        $options = array();
        if ($definition->getType() == 'choice') {
            $options['identifier'] = $identifier;
        }

        $parentData = null;
        if (null !== $this->parentDatas && null !== $characteristic = $this->parentDatas->findCharacteristicByIdentifier($identifier)) {
            $parentData = $characteristic->display($definition);
        }

        $form->add($identifier, $type, array_merge(array(
            'property_path' => '[' . $identifier . ']',
            'label' => $definition->getTitle(),
            'parent_data' => $parentData,
        ), $options));
    }
1210532
public function createFormView(FormInterface $form)
    {
        $post = $this->eavManager->initializeEntity($form->getSchema());

        $form = $this->formFactory->create(PostType::class, $post, ['form_id' => $form->getId()]);

        return $form->createView();
    }
136077
public function transform(FormInterface $form)
    {
        $question = parent::transform($form);
        $question->setHidden(true);

        return $question;
    }
7700
public function interactWith(
        FormInterface $form,
        HelperSet $helperSet,
        InputInterface $input,
        OutputInterface $output
    ) {
        if (!$input->isInteractive()) {
            throw new CanNotInteractWithForm('This interactor only works with interactive input');
        }

        if (!FormUtil::isCompound($form)) {
            throw new CanNotInteractWithForm('Expected a compound form');
        }

        $submittedData = [];

        foreach ($form->all() as $name => $field) {
            try {
                $submittedData[$name] = $this->formInteractor->interactWith($field, $helperSet, $input, $output);
            } catch (NoNeedToInteractWithForm $exception) {
                continue;
            }
        }

        return $submittedData;
    }
433175
protected function getErrorsFromForm(FormInterface $form)
    {
        $errors = [];
        /** @var FormError $error */

        if($form->getErrors()->count() === 0 && $form->isRoot() && !$form->isValid()){
            $errors["_self"] = "Empty post data";
        }

        foreach ($form->getErrors() as $error) {
            $key = $error->getOrigin()->getName();
            $errors[($form->isRoot()) ? "_self" : $key] = $error->getMessage();
        }

        foreach ($form->all() as $childForm) {
            if ($childForm instanceof FormInterface) {
                if ($childErrors = $this->getErrorsFromForm($childForm)) {
                    unset($errors["_self"]);
                    $errors = array_merge_recursive($errors, $childErrors);
                }
            }
        }

        return $errors;
    }
1346742
private function getFormErrors($errors, $formConfig)
    {
        foreach ($errors as $errorKey => $errorValue) {
            foreach ($formConfig['elements'] as $elementKey => $elementValue) {
                if ($elementValue['spec']['name'] == $errorKey && !empty($elementValue['spec']['options']['label'])) {
                    $errors[$elementValue['spec']['options']['label']] = reset($errorValue);
                    unset($errors[$errorKey]);
                }
            }
        }

        return $errors;
    }
397427
private function getFormInterface(FormInterface $form, $id): FormInterface
    {
        $name = $form->getName();
        if ($id === $name)
            return $form;
        if (mb_strpos($id, $name.'_') === 0) {
            $id = mb_substr($id, mb_strlen($name . '_'));
            foreach ($form->all() as $name => $child) {
                if ($id === $name)
                    return $child;
                if (mb_strpos($id, $name.'_') === 0)
                    return $this->getFormInterface($child, $id);
            }
        }
        return $form;
    }
1374810
protected function addAnswersFields(FormInterface $form)
    {
        $question = $this->questionInteract->getQuestion();
        $data = $this->getQuestionData();

        /** @var AnswerCheckerInterface $checker */
        $checker = $this->checkerLocator->get($question->getType());
        $options = $checker->getOptions($question->getAnswers()->getValues(), $data);
        $form->add('answers', $checker->getType(), $options);
    }
653452
protected function overrideAlternative(FormInterface $form)
    {
        $needFlush = false;
        foreach ($this->thumbnailConfig as $format => $parameters) {
            $file = $form->get('files')->get($format)->get('file')->getData();
            if (null !== $file) {
                $media = $form->getData();
                $tmpFileName = time() . '-' . $file->getClientOriginalName();
                $file->move($this->tmpDir, $tmpFileName);
                $tmpFilePath = $this->tmpDir . DIRECTORY_SEPARATOR . $tmpFileName;
                $this->imageAlternativeStrategy->overrideAlternative($media, $tmpFilePath, $format);
               $this->objectManager->persist($media);
                $needFlush = true;
            }
        }
        if ($needFlush) {
            $this->objectManager->flush();
        }
    }
1234750
public function addReferrerToForm(FormInterface $form): void
    {
        $request = $this->get('request_stack')->getCurrentRequest();
        $form->add('http-referrer', HiddenType::class, [
            'mapped' => false,
            'data' => $request->headers->get('referer'),
        ]);
    }
1013839
protected function _recurseForm(\Zend_Form $form)
 {
        $subFormsWithErrors = array();
        $subFormMessages = array();
        $tabId = 0;

        foreach ($form->getSubForms() as $subForm) {
            if ($subForm instanceof \Gems_Form_TabSubForm) {
                // See if any of the subformelements has an error message
                foreach ($subForm->getElements() as $subFormElement) {
                    $elementMessages = $subFormElement->getMessages();
                    if (count($elementMessages)) {
                        $subFormsWithErrors[$tabId] = $subForm->getAttrib('title'); // Save subform title
                        $subForm->setAttrib('jQueryParams', array('class' => 'taberror'));    // Add css class to the subform
                        $form->selectTab($tabId);   // Select the tab, this way the last tab with error is always selected
                        break;  // don't check other elements
                    }
                }

                // Preserve subform level custom messages if we have an error
                if (array_key_exists($tabId, $subFormsWithErrors)) {
                    $subFormMessages[$tabId] = $subForm->getCustomMessages();
                }
                $tabId++;
            }
        }

        // If we found at least one error, and 'verbose' is true
        if ($this->getVerbose() && (!empty($subFormsWithErrors) || $form->isErrors())) {
            // First show form level custom error messages (the elements show their own errors)
            $formMessage = $form->getCustomMessages();
            if (!empty($formMessage)) {
                foreach ($formMessage as $message)
                {
                    \Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger')->addMessage($message);
                }
            }

            // Now browse through the tabs with errors
            foreach ($subFormsWithErrors as $tabIdx => $tabName)
            {
                // If more then one tab, show in which tab we found the errors
                if ($tabId > 1) {
                    $translator = \Zend_Registry::get('Zend_Translate');
                    \Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger')->addMessage(sprintf($translator->_('Error in tab "%s"'), $tabName));
                }

                // If we have them, show the tab custom error messages
                foreach ($subFormMessages[$tabIdx] as $subFormMessage)
                {
                    foreach ($subFormMessage as $message)
                    {
                        \Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger')->addMessage("--> " . $message);
                    }
                }
            }
        }
    }
255341
public function saveStep(FormInterface $form)
    {
        $answers = $this->getUserConfiguration()->getAnswers();
        $data = $form->getData();
        $answersData = $form->get('answers')->getData();

        if (is_null($answersData)) {
            $answersData = array();
        } else if (!is_array($answersData)) {
            $answersData = array($answersData);
        }

        $answers[$this->getCurrentQuestion()] = array_merge($answersData, $data);
        $this->getUserConfiguration()->setAnswers($answers);
        $this->updateSessionConfiguration();

        return $this;
    }
1090110
protected function translationDomainFrom(FormInterface $form)
    {
        while ((null === $domain = $form->getConfig()->getOption('translation_domain')) && $form->getParent()) {
            $form = $form->getParent();
        }

        return $domain;
    }
294316
private function convertFormToArray(FormInterface $data)
    {
        $form = $errors = [];
        foreach ($data->getErrors() as $error) {
            $errors[] = $this->getErrorMessage($error);
        }

        if ($errors) {
            $form['errors'] = $errors;
        }

        $children = [];
        foreach ($data->all() as $child) {
            if ($child instanceof FormInterface) {
                $children[$child->getName()] = $this->convertFormToArray($child);
            }
        }

        if ($children) {
            $form['children'] = $children;
        }

        return $form;
    }