Created
February 16, 2016 10:03
-
-
Save lorenzo/3c9359300ff422d0a14c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
namespace App\Form; | |
use JsonSerializable; | |
class ErrorBag implements JsonSerializable | |
{ | |
protected $_errors; | |
public function __construct($errors) | |
{ | |
$this->_errors = $errors; | |
} | |
public function jsonSerialize() | |
{ | |
$result = []; | |
foreach ($this->_errors as $field => $error) { | |
$result[] = [ | |
'code' => key($error), | |
'parameter' => $field, | |
'detail' => current($error) | |
]; | |
} | |
return $result; | |
} | |
} |
This file contains hidden or 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 | |
$entityWithErrors = ...; | |
$errors = new ErrorBag($entityWithErrors->errors()); | |
$this->set('errors', $errors); | |
$this->set('_serialize', ['errors']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment