Skip to content

Instantly share code, notes, and snippets.

@RomainMarecat
Last active June 9, 2016 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RomainMarecat/bd66235d7d33165bebb1603e5d1cb77f to your computer and use it in GitHub Desktop.
Save RomainMarecat/bd66235d7d33165bebb1603e5d1cb77f to your computer and use it in GitHub Desktop.
Form Parser error and view for Symfony 2 Restful API
<?php
/**
* AppBundle\Services\FormParser.php
*/
namespace AppBundle\Services;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
/**
* @author Romain Marecat <romain.marecat@gmail.com>
*/
class FormParser
{
/**
* Pass form object and call it to get resulting array.
*
* @param FormInterface $form
* @return array
*/
public function parseErrors(FormInterface $form)
{
$results = array();
return $this->realParseErrors($form, $results);
}
/**
* Pass form object and call it to get view resulting array
* @param FormInterface $form
* @return array
*/
public function parseView(FormInterface $form)
{
$results = array();
return $this->realParseView($form, $results);
}
/**
* This does the actual job. Method travels through all levels of form recursively and gathers errors.
*
* @param FormInterface $form
* @param array &$results
* @return array
*/
private function realParseView(FormInterface $form, array $results)
{
$view = $form->createView();
if ($view instanceof FormView) {
$config = $form->getConfig();
$results[$view->vars['name']] = array_merge(
self::getViewData($view->vars),
array(
'submitted' => $form->isSubmitted(),
'valid' => $form->isValid(),
'method' => $config->getMethod(),
'action' => $config->getAction(),
)
);
}
self::parseChildrenView($view, $results);
return $results;
}
/**
* parseChildrenView
* @param FormView $view
* @param array &$results
* @return array
*/
protected static function parseChildrenView($view, array &$results)
{
if ($view->count() > 0) {
$children = $view->children;
foreach ($children as $child) {
if ($child instanceof FormView) {
$results[$view->vars['name']][$child->vars['name']] = self::getViewData($child->vars);
self::parseChildrenView($child, $results[$view->vars['name']]);
}
}
}
}
/**
* getViewData
* @param array $vars
* @return array
*/
private static function getViewData(array $vars)
{
return array(
'required' => isset($vars['required']) ? $vars['required'] : null,
'value' => isset($vars['value']) ? $vars['value'] : null,
);
}
/**
* This does the actual job. Method travels through all levels of form recursively and gathers errors.
*
* @param FormInterface $form
* @param array &$results
* @return array
*/
private function realParseErrors(FormInterface $form, array &$results)
{
foreach ($form->getErrors(true, true) as $key1 => $errors) {
$key = $errors->getOrigin() ? $errors->getOrigin()->getName() : $key1;
$results[$key][] = $errors->getMessage();
}
return $results;
}
}
# AppBundle\Resources\config\services.yml
services:
services.form_parser:
class: AppBundle\Services\FormParser
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment