Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
Merge pull request #4 in SUNSHINE/formz from bugfix/SUN-4277-formz-er…
Browse files Browse the repository at this point in the history
…ror-ajax to wip/steps

* commit '16bc0228b8c643fb4e9507bb15d0a2393021c197':
  [FEATURE] Dispatch signal when error occurs during Ajax validation
  [BUGFIX] Handle data mapping error in Ajax validation
  • Loading branch information
Romain Canon authored and Romain Canon committed Oct 17, 2018
2 parents f019be5 + 16bc022 commit 82fcb64
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Classes/Controller/AjaxValidationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Romm\Formz\Exceptions\ClassNotFoundException;
use Romm\Formz\Exceptions\EntryNotFoundException;
use Romm\Formz\Exceptions\InvalidArgumentTypeException;
use Romm\Formz\Exceptions\InvalidArgumentValueException;
use Romm\Formz\Exceptions\InvalidConfigurationException;
use Romm\Formz\Exceptions\MissingArgumentException;
use Romm\Formz\Form\Definition\Field\Validation\Validator;
Expand Down Expand Up @@ -49,6 +50,7 @@
class AjaxValidationController extends ActionController
{
const DEFAULT_ERROR_MESSAGE_KEY = 'default_error_message';
const MAPPING_ERROR = 'MappingError';

/**
* @var Request
Expand Down Expand Up @@ -229,6 +231,24 @@ public function runAction($name, $className, $fieldName, $validatorName, $formzD
$this->result->merge($result);
}

/**
* If an error occurs, that must be because the mapping of the arguments
* failed somehow. Therefore we override the default behaviour (forward to
* referring request) and we throw an exception instead.
*
* @throws InvalidArgumentValueException
*/
public function errorAction()
{
$this->signalSlotDispatcher->dispatch(
__CLASS__,
self::MAPPING_ERROR,
[$this->arguments->getValidationResults()]
);

throw InvalidArgumentValueException::ajaxDataMapperError($this->arguments->getValidationResults()->getFlattenedErrors());
}

/**
* Will call all middlewares of the form.
*
Expand Down
26 changes: 26 additions & 0 deletions Classes/Exceptions/InvalidArgumentValueException.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Romm\Formz\Middleware\Signal\SendsMiddlewareSignal;

use Romm\Formz\Form\FormInterface;
use TYPO3\CMS\Extbase\Error\Error;

class InvalidArgumentValueException extends FormzException
{
Expand All @@ -25,6 +26,8 @@ class InvalidArgumentValueException extends FormzException

const FORM_NAME_EMPTY = 'The name of the form (type: "%s") can not be empty.';

const AJAX_DATA_MAPPER_ERROR = 'Arguments mapping validation error %s';

/**
* @code 1485786285
*
Expand Down Expand Up @@ -74,4 +77,27 @@ final public static function formNameEmpty(FormInterface $form)

return $exception;
}

/**
* @code 1539693830
*
* @param Error[] $errorsList
* @return self
*/
final public static function ajaxDataMapperError(array $errorsList)
{
$message = '';

foreach ($errorsList as $key => $errors) {
$message .= ' / ' . $key . ': ' . implode('; ', $errors);
}

/** @var self $exception */
$exception = self::getNewExceptionInstance(
self::AJAX_DATA_MAPPER_ERROR,
[$message]
);

return $exception;
}
}

0 comments on commit 82fcb64

Please sign in to comment.