private function convertFormToArray(GenericSerializationVisitor $visitor, Form $data) { $isRoot = null === $visitor->getRoot(); $form = new \ArrayObject(); $errors = []; foreach ($data->getErrors() as $error) { $errors[] = $this->getErrorMessage($error); } if (!empty($errors)) { $form['errors'] = $errors; } $children = []; foreach ($data->all() as $child) { if ($child instanceof Form) { $children[$child->getName()] = $this->convertFormToArray($visitor, $child); } } if (!empty($children)) { $form['children'] = $children; } if ($isRoot) { $visitor->setRoot($form); } return $form; }
protected function getErrorMessages(\Symfony\Component\Form\Form $form, $name) { $errors = array(); foreach ($form->getErrors() as $key => $error) { $errors[] = $error->getMessage(); } foreach ($form->all() as $child) { $type = $child->getConfig()->getType()->getName(); if ($child->count() && $type !== 'choice') { $childErrors = $this->getErrorMessages($child, $child->getName()); if (sizeof($childErrors)) { $errors = array_merge($errors, $childErrors); } } else { if (!$child->isValid()) { if ($name == "responsable1" || $name == "responsable2") { $errors[$child->getParent()->getParent()->getName() . '_' . $name . '_' . $child->getName()] = $this->getErrorMessages($child, $child->getName()); } else { $errors[$name . '_' . $child->getName()] = $this->getErrorMessages($child, $child->getName()); } } } } return $errors; }
public function stringifyFormErrors(Form $form) { $errors = []; foreach ($form->getErrors(true, true) as $error) { $errors[] = $error->getMessage(); } return implode('<br>', $errors); }
public function createView(Form $form, $template, $parameters, $extraParameters = []) { $html = $this->templating->render($template, $parameters); $response = new JsonResponse(); $response->setData(array_merge($extraParameters, ['submitted' => $form->isSubmitted(), 'errors' => count($form->getErrors(true)), 'html' => $html])); return $response; }
/** * Gets a form error messages * * @param Form $form * * @return array|string */ public static function getFormErrorMessages(Form $form) { $errors = []; foreach ($form->getErrors(true, true) as $error) { $errors = $error->getMessage(); } return $errors; }
private function getErrorMessages(Form $form) { $errors = []; foreach ($form->getErrors(true, true) as $key => $error) { $errors[] = $error->getMessage(); } return $errors; }
public function stringifyFormErrors(Form $form) { $errors = []; foreach ($form->getErrors(TRUE, TRUE) as $error) { $errors[] = $error->getMessage(); } //return implode(', ', $errors); return !empty($errors[0]) ? $errors[0] : NULL; }
/** * Method to add form errors to the response array * * @param Form $form * @return \AppBundle\Controller\AppController */ protected function addFormErrors(Form $form) { $response = array(); $response['errorCode'] = self::FORM_ERRORS; foreach ($form->getErrors(true) as $error) { $response['errorMessage'][] = $error->getMessage(); } $this->response = $response; return $this; }
/** * フォームのバリデーションを行う * ※ServiceクラスでEntityのバリデーションを行っているが、 * FormTypeクラスで設定したチェックや、CSRFトークンのチェックは * $form->isValid()でないと検証できないため、 * フォームの検証の場合、本メソッドを使用すること * * @param Form $form * @throws FormValidationException */ private function tryFormValidate(Form $form) { if (!$form->isValid()) { $exception = new FormValidationException(); foreach ($form->getErrors(true, true) as $error) { $exception->addErrorMessage($error->getMessage()); } throw $exception; } }
/** * @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); }
public function getFormErrorsArray(Form $form, $prefix = '') { $errors = array(); $errors['count'] = count($form->getErrors()); $errors['count_deep'] = count($form->getErrors(true)); if (count($form->getErrors()) > 0) { $errors['global'] = array(); } /** * @var FormError $globalError */ foreach ($form->getErrors() as $globalError) { $errors['global'][] = $this->translator->trans($globalError->getMessage()); } /** * @var FormInterface $child */ foreach ($form->all() as $name => $child) { if (count($child->getErrors()) > 0) { $errors[sprintf('%s%s', $prefix, $name)] = array(); } /** * @var FormError $error */ foreach ($child->getErrors() as $error) { $errors[sprintf('%s%s', $prefix, $name)][] = $this->translator->trans($error->getMessage()); } if (count($child->all()) > 0) { foreach ($child->all() as $nameChild => $childChild) { $name = sprintf('%s_%s', $name, $nameChild); if (count($childChild->getErrors()) > 0) { $errors[sprintf('%s%s', $prefix, $name)] = array(); } foreach ($childChild->getErrors() as $error) { $errors[sprintf('%s%s', $prefix, $name)][] = $this->translator->trans($error->getMessage()); } } } } return $errors; }
/** * @return array */ protected function getErrors() { $errors = []; $generalErrors = []; foreach ($this->form->getErrors() as $error) { $generalErrors[] = $error->getMessage(); } if (!empty($generalErrors)) { $errors['general'] = $generalErrors; } foreach ($this->form->all() as $field) { $fieldErrors = []; foreach ($field->getErrors() as $error) { $fieldErrors[] = $error->getMessage(); } if (!empty($fieldErrors)) { $errors[$field->getName()] = $fieldErrors; } } return $errors; }
/** * @param Form $form The form * @param string $namePrefix The field name prefix (concatenation of parents names). * @return array The form errors, as an array */ private function getErrorsArray(Form $form, $namePrefix = '') { $formName = $namePrefix ? sprintf('%s[%s]', $namePrefix, $form->getName()) : $form->getName(); $errors = array(); foreach ($form->getErrors() as $error) { if (!isset($errors[$formName])) { $errors[$formName] = array(); } $errors[$formName][] = $error->getMessage(); } return $errors; }
/** * @param Form $form * * @return array */ public function getErrorMessages(Form $form) { $errors = array(); foreach ($form->getErrors() as $key => $error) { $errors[] = $error->getMessage(); } foreach ($form->all() as $child) { if (!$child->isValid()) { $errors[$child->getName()] = $this->getErrorMessages($child); } } return $errors; }
public function serializeFormErrors(\Symfony\Component\Form\Form $form, $flat_array = false, $add_form_name = false, $glue_keys = '_') { $errors = array(); $errors['global'] = array(); $errors['fields'] = array(); foreach ($form->getErrors() as $error) { $errors['global'][] = $error->getMessage(); } $errors['fields'] = $this->serialize($form); if ($flat_array) { $errors['fields'] = $this->arrayFlatten($errors['fields'], $glue_keys, $add_form_name ? $form->getName() : ''); } return $errors; }
protected function getErrorMessages(Form $form) { $errors = array(); foreach ($form->getErrors() as $key => $error) { $errors[$key] = $error->getMessage(); } foreach ($form->all() as $child) { if (!$child->isValid()) { $key = sprintf('%s[%s]', $form->getName(), $child->getName()); $errors[$key] = $this->getErrorMessages($child); } } return $errors; }
/** * @return array */ public function getFormErrors() { $errors = array(); foreach ($this->form->getErrors() as $key => $error) { $errors[$key] = $error->getMessage(); } // Get errors from children foreach ($this->form->all() as $child) { if (!$child->isValid()) { $errors[$child->getName()] = $this->getFormErrors($child); } } return $errors; }
public static function processFormErrorsDeeply(Form $form, array &$errors) { $children = $form->all(); if (!empty($children)) { foreach ($children as $childForm) { self::processFormErrorsDeeply($childForm, $errors); } } // get the errors $formErrors = $form->getErrors(); foreach ($formErrors as $err) { $errors[] = $err->getMessage(); } return; }
/** * Retrieves the errors for all form fields recursively * * @param Form $form * @return array */ protected function getErrorsFromForm(Form $form) { $errors = array(); foreach ($form->getErrors() as $error) { $errors[] = $error->getMessage(); } foreach ($form->all() as $childForm) { if ($childForm instanceof Form) { if ($childErrors = $this->getErrorsFromForm($childForm)) { $errors[$childForm->getName()] = $childErrors; } } } return $errors; }
/** * Get all errors that occurred in a form * * @param \Symfony\Component\Form\Form $form * @return string the error string */ public function getErrorMessages(Form $form) { $errors = ''; foreach ($form->getErrors() as $key => $error) { $errors .= $error->getMessage() . ', '; } /** @var Form $child */ foreach ($form->all() as $child) { if (!$child->isValid()) { $fieldName = $child->getConfig()->getOption('label', $child->getName()); $errors .= sprintf("[%s] %s, ", $fieldName, $this->getErrorMessages($child)); } } return rtrim($errors, ', '); }
private function getErrorMessages(\Symfony\Component\Form\Form $form) { $errors = array(); foreach ($form->getErrors() as $key => $error) { $errors[] = $error->getMessage(); } foreach ($form->all() as $child) { if (!$child->isValid()) { $_errors = $this->getErrorMessages($child); if ($_errors) { $errors[$child->getName()] = $_errors; } } } return $errors; }
/** * @param Form $form * @return array */ protected function getFormErrors(Form $form) { $errors = array(); // Global foreach ($form->getErrors() as $error) { $errors[$form->getName()][] = $error->getMessage(); } // Fields foreach ($form as $child) { if (!$child->isValid()) { foreach ($child->getErrors() as $error) { $errors[$child->getName()][] = $error->getMessage(); } } } return $errors; }
protected function validateForm(Form $form, $request_data) { $form->bind($request_data); if (!$form->isValid()) { $msg = $form->getErrors(); if (sizeOf($msg)) { $msg = array_map(function ($message) { return $message->getMessage(); }, $msg); $msg = "\n- " . implode("\n- ", $msg); } else { $msg = "\n- " . $form->getErrorsAsString(); } $this->get('logger')->err('Erreur formulaire: ' . $msg); throw new InvalidArgumentException($msg); } }
private function getFormErrorMessages(Form $form) { $errors = []; foreach ($form->getErrors() as $key => $error) { if ($form->isRoot()) { $errors['#root'][] = $error->getMessage(); } else { $errors[] = $error->getMessage(); } } foreach ($form->all() as $child) { if (!$child->isValid()) { $errors[$child->getName()] = $this->getFormErrorMessages($child); } } return $errors; }
/** * This method comes from Flip's answer on Stackoverflow: * http://stackoverflow.com/a/17428869/731138 * * @param Form $form * @return array */ protected function getErrorMessages(Form $form) { $errors = array(); foreach ($form->getErrors() as $error) { if ($form->isRoot()) { $errors['#'][] = $error->getMessage(); } else { $errors[] = $error->getMessage(); } } foreach ($form->all() as $child) { if (!$child->isValid()) { $errors[$child->getName()] = $this->getErrorMessages($child); } } return $errors; }
/** * @param Form $form * @return array */ protected function getErrorMessages(Form $form) { $errors = []; foreach ($form->getErrors() as $key => $error) { if ($form->isRoot()) { $errors[] = $this->toErrorArray($error); } else { $errors[] = $this->toErrorArray($error, $form); } } foreach ($form->all() as $child) { if (!$child->isValid()) { foreach ($this->getErrorMessages($child) as $error) { $errors[] = $error; } } } return $errors; }
/** * Returns form errors for JS implementation. * * Parse all errors mapped by id html field * * @param Form $form The form * @return array[array[string]] Errors */ public function getFormErrorsForJS(Form $form) { $errors = []; if (empty($form)) { return $errors; } $translator = $this->container->get('translator'); foreach ($form->getErrors(true) as $error) { if (!$error->getCause()) { $form_id = 'bubbling_errors'; } else { $form_id = str_replace(['.', 'children[', ']', '_data'], ['_', '', '', ''], $error->getCause()->getPropertyPath()); } if ($error->getMessagePluralization()) { $errors[$form_id][] = $translator->transchoice($error->getMessageTemplate(), $error->getMessagePluralization(), $error->getMessageParameters(), 'form_error'); } else { $errors[$form_id][] = $translator->trans($error->getMessageTemplate(), $error->getMessageParameters(), 'form_error'); } } return $errors; }
public static function getAllErrors(Form $form, $parent_prefix = '') { $id = $parent_prefix . ($parent_prefix ? '_' : '') . $form->getName(); $errors = array($id . '_errors' => array()); foreach ($form->getErrors() as $error) { $errors[$id . '_errors'][] = array('error' => self::formatErrors($error)); } if (!count($errors[$id . '_errors'])) { unset($errors[$id . '_errors']); } foreach ($form->getChildren() as $child) { if ($_errors = self::getAllErrors($child, $id)) { $errors = array_merge($errors, $_errors); } } if (count($errors)) { return $errors; } else { return false; } }
public function getFormErrorMessages(\Symfony\Component\Form\Form $form) { $errors = array(); $formErrors = iterator_to_array($form->getErrors(false, true)); foreach ($formErrors as $key => $error) { $template = $error->getMessageTemplate(); $parameters = $error->getMessageParameters(); foreach ($parameters as $var => $value) { $template = str_replace($var, $value, $template); } $errors[$key] = $template; } if ($form->count()) { foreach ($form as $child) { if (!$child->isValid()) { $errors[$child->getName()] = $this->getFormErrorMessages($child); } } } return $errors; }
public function getFormErrorMessagesWithLabels(Form $form, FormHelper $formHelper) { $errors = array(); foreach ($form->getErrors() as $key => $error) { if ($form->isRoot()) { $errors['#'][] = $error->getMessage(); } else { $errors[] = $error->getMessage(); } } foreach ($form->all() as $child) { if (!$child->isValid()) { $label = $child->getConfig()->getOption('label'); if (!$label) { $label = $formHelper->humanize($child->getName()); } $errors[$label] = $this->getFormErrorMessagesWithLabels($child, $formHelper); } } return $errors; }