Skip to content

Commit

Permalink
Improved form validation JSON responses to contain list of failed fie…
Browse files Browse the repository at this point in the history
…lds with their error messages
  • Loading branch information
mahagr committed Nov 12, 2021
1 parent d9c9f6a commit e6911ce
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Added `route` and `request` to `onPagesInitialized` event
* Improved page cloning, added method `Page::initialize()`
* Improved `FlexObject::getChanges()`: return changed lists and arrays as whole instead of just changed keys/values
* Improved form validation JSON responses to contain list of failed fields with their error messages
3. [](#bugfix)
* Fixed path traversal vulnerability when using `bin/grav server`
* Fixed unescaped error messages in JSON error responses
Expand Down
23 changes: 19 additions & 4 deletions system/src/Grav/Common/Data/ValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@
namespace Grav\Common\Data;

use Grav\Common\Grav;
use JsonSerializable;
use RuntimeException;

/**
* Class ValidationException
* @package Grav\Common\Data
*/
class ValidationException extends RuntimeException
class ValidationException extends RuntimeException implements JsonSerializable
{
/** @var array */
protected $messages = [];
protected $escape = true;

/**
* @param array $messages
Expand All @@ -32,21 +34,34 @@ public function setMessages(array $messages = [])
$language = Grav::instance()['language'];
$this->message = $language->translate('GRAV.FORM.VALIDATION_FAIL', null, true) . ' ' . $this->message;

foreach ($messages as $variable => &$list) {
foreach ($messages as $list) {
$list = array_unique($list);
foreach ($list as $message) {
$this->message .= "<br/>$message";
$this->message .= '<br/>' . htmlspecialchars($message, ENT_QUOTES | ENT_HTML5, 'UTF-8');
}
}

return $this;
}

public function setSimpleMessage(bool $escape = true): void
{
$first = reset($this->messages);
$message = reset($first);

$this->message = $escape ? htmlspecialchars($message, ENT_QUOTES | ENT_HTML5, 'UTF-8') : $message;
}

/**
* @return array
*/
public function getMessages()
public function getMessages(): array
{
return $this->messages;
}

public function jsonSerialize(): array
{
return ['validation' => $this->messages];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Grav\Framework\Psr7\Response;
use Grav\Framework\RequestHandler\Exception\RequestException;
use Grav\Framework\Route\Route;
use JsonSerializable;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
Expand Down Expand Up @@ -209,14 +210,17 @@ protected function getErrorJson(Throwable $e): array
} else {
$message = htmlspecialchars($e->getMessage(), ENT_QUOTES | ENT_HTML5, 'UTF-8');
}

$extra = $e instanceof JsonSerializable ? $e->jsonSerialize() : [];

$response = [
'code' => $code,
'status' => 'error',
'message' => $message,
'error' => [
'code' => $code,
'message' => $message
]
] + $extra
];

/** @var Debugger $debugger */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Grav\Common\Grav;
use Grav\Framework\Psr7\Response;
use JsonException;
use JsonSerializable;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
Expand Down Expand Up @@ -46,14 +47,17 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
} else {
$message = htmlspecialchars($exception->getMessage(), ENT_QUOTES | ENT_HTML5, 'UTF-8');
}

$extra = $exception instanceof JsonSerializable ? $exception->jsonSerialize() : [];

$response = [
'code' => $code,
'status' => 'error',
'message' => $message,
'error' => [
'code' => $code,
'message' => $message,
]
] + $extra
];

/** @var Debugger $debugger */
Expand Down

0 comments on commit e6911ce

Please sign in to comment.