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; }
/** * Get rid on any fields that don't appear in the form * * @param Request $request * @param Form $form */ protected function removeExtraFields(Request $request, Form $form) { $data = $request->request->all(); $children = $form->all(); $data = array_intersect_key($data, $children); $request->request->replace($data); }
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; }
/** * @param Form $form The form subject to validation * @param string $prefix The field name prefix (concatenation of parents names). * @return array */ private function getErrorsFromSubForm(Form $form, $prefix) { $errors = array(); foreach ($form->all() as $child) { $errors = array_merge($errors, $this->getErrorsArray($child, $prefix), $this->getErrorsFromSubForm($child, sprintf('%s[%s]', $prefix, $child->getName()))); } return $errors; }
/** * {@inheritdoc} */ protected function groupChild(Form $form) { $group_child = array(); foreach ($form->all() as $child) { $group_child[substr($child->getName(), 0, -1)][] = $child; } return $group_child; }
/** * @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; }
/** * @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; }
/** * Returns instance of SymfonyForm and fills it with fields from $_fields. * Checks whether all $_fields are already in the SymfonyForm and adds * them if necessary. * * @return SymfonyForm Returns assigned form */ public function getForm() { if (!$this->_form) { $this->_form = $this->_initialiseForm(); } $formFields = $this->_form->all(); foreach ($this->_fields as $name => &$fieldArray) { if (!array_key_exists($name, $formFields)) { $this->_addFieldToSymfonyForm($fieldArray); } } return $this->_form; }
/** * This function remove fields available if there are not present in the $data array * The data array might come from $request->request->all(). * * This can be usefull if you don't want to send all fields will building an api. As missing * fields will be threated like null values. * * @param array $data * @param Form $form */ public static function removeFields(array $data, Form $form) { $diff = array_diff(array_keys($form->all()), array_keys($data)); foreach ($diff as $key) { $form->remove($key); } foreach ($data as $name => $value) { if (!is_array($value)) { continue; } self::removeFields($value, $form[$name]); } }
/** * @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 getErrorMessages(BaseForm $form) { $errorMessages = []; if (!$form->isValid()) { $formData = $form->all(); foreach ($formData as $name => $item) { if (!$item->isValid()) { $errorMessages[] = $name . ' - ' . $item->getErrors(true); } } } return implode(PHP_EOL, $errorMessages); }
/** * @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; }
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; }
/** * Method to remove unwanted parameters in the request. * * @param array $requestData * @param Form $form * @return multitype:unknown */ protected function sanitizeInput($requestData, Form $form) { $filteredInput = array(); foreach ($form->all() as $childName => $child) { if ($child->count() > 0 && isset($requestData[$childName])) { foreach ($child as $grandChildName => $grandChild) { $filteredInput[$childName][$grandChildName] = $this->sanitizeInput($requestData[$childName], $grandChild); } } if (isset($requestData[$childName])) { $filteredInput[$childName] = $requestData[$childName]; } } return $filteredInput; }
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; }
/** * 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; }
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; }
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; }
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; }
public function getFormErrors(\Symfony\Component\Form\Form $form) { $errors = array(); if ($form->count() > 0) { foreach ($form->all() as $child) { /** * @var \Symfony\Component\Form\Form $child */ if (!$child->isValid()) { $errors[$child->getName()] = $this->getFormErrors($child); } } } else { /** * @var \Symfony\Component\Form\FormError $error */ foreach ($form->getErrors() as $key => $error) { $errors[] = $error->getMessage(); } } return $errors; }
/** * Create a default node hierarchy by using AND operator. * * @param Form $form * @param ConditionNodeInterface $root * @param string $parentName */ protected function buildDefaultConditionNode(Form $form, ConditionNodeInterface $root, $parentName = '') { foreach ($form->all() as $child) { $name = '' !== $parentName ? $parentName . '.' . $child->getName() : $child->getName(); if ($child->getConfig()->hasAttribute('add_shared')) { $isCollection = $child->getConfig()->getType()->getInnerType() instanceof CollectionAdapterFilterType; $this->buildDefaultConditionNode($isCollection ? $child->get(0) : $child, $root->andX(), $name); } else { $root->field($name); } } }
/** * Get all errors that occured in a form * * @param \Symfony\Component\Form\Form $form * @return string the error string */ private function getErrorMessages(\Symfony\Component\Form\Form $form) { $errors = ''; foreach ($form->getErrors() as $key => $error) { $errors .= $error->getMessage() . ', '; } 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, ', '); }
protected function api_get_form_errors(\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[$child->getName()] = $this->api_get_form_errors($child); } } return $errors; }
public function getFormErrorMessages(\Symfony\Component\Form\Form $form) { $errors = []; foreach ($form->getErrors() as $key => $error) { if ($form->isRoot()) { $errors['#'][] = $error->getMessage(); } else { $errors[] = $error->getMessage(); } } foreach ($form->all() as $child) { if (!$child->isValid()) { $errors[$child->getName()] = $this->getFormErrorMessages($child); } } return $errors; }
/** * Extract the updated job configuration from the job steps form * * @param Form $form * * @return array */ protected function extractJobConfiguration(Form $form) { $configuration = []; foreach ($form->all() as $stepForm) { foreach ($stepForm->all() as $stepElementForm) { foreach ($stepElementForm->all() as $element => $valueForm) { $configuration[$element] = $valueForm->getData(); } } } return $configuration; }
/** * @return string|null */ private function getErrorMessage(Form $form) { foreach ($form->all() as $child) { foreach ($child->getErrors() as $error) { return $error->getMessage(); } } foreach ($form->getErrors() as $error) { return $error->getMessage(); } return "Unknown Error"; }
/** * Get validation error from form * * @param \Symfony\Component\Form\Form $form the form * @return array errors messages */ public function getErrorMessages(Form $form) { $errors = array(); foreach ($form->getErrors() as $key => $error) { $template = $error->getMessageTemplate(); $parameters = $error->getMessageParameters(); foreach ($parameters as $var => $value) { $template = str_replace($var, $value, $template); } $errors[$key] = $template; } foreach ($form->all() as $key => $child) { /** @var $child Form */ if ($err = $this->getErrorMessages($child)) { $errors[$key] = $err; } } return $errors; }