Created
March 16, 2014 20:55
-
-
Save ptlis/9589774 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* PHP Version 5.3 | |
* | |
* @copyright (c) 2014 brian ridley | |
* @author brian ridley <[email protected]> | |
* @license http://opensource.org/licenses/MIT MIT | |
* | |
* For the full copyright and license information, please view the LICENSE | |
* file that was distributed with this source code. | |
*/ | |
namespace ptlis\CoverageProxyBundle\Form; | |
use Symfony\Component\Form\FormInterface; | |
/** | |
* Provides a convenient serialization from Symfony form errors to an array. | |
*/ | |
class FormErrorSerializer | |
{ | |
/** | |
* Convert the form errors to an array representation. | |
* | |
* @param FormInterface $form | |
* | |
* @return string[]|array[] | |
*/ | |
public function toArray(FormInterface $form) | |
{ | |
$errorList = array(); | |
if (count($form->all())) { | |
foreach ($form->all() as $child) { | |
if (!$child->isValid()) { | |
$innerErrorList = $this->toArray($child); | |
if (count($innerErrorList)) { | |
$errorList[$child->getName()] = $innerErrorList; | |
} | |
} | |
} | |
} else { | |
foreach ($form->getErrors() as $error) { | |
$errorList[] = $error->getMessage(); | |
} | |
} | |
return $errorList; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment