From b06d3e0eba0b358ffa5992e6a6c17a2cdfbc54c5 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Wed, 16 Sep 2015 11:59:14 +0900 Subject: [PATCH] "ValidationFailed" method suffix for validation failed method --- src/OnFailureMethodHandler.php | 10 ++++++++-- tests/Fake/FakeController.php | 10 ++++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/OnFailureMethodHandler.php b/src/OnFailureMethodHandler.php index 9d40a66..2fe91f9 100644 --- a/src/OnFailureMethodHandler.php +++ b/src/OnFailureMethodHandler.php @@ -13,6 +13,8 @@ final class OnFailureMethodHandler implements FailureHandlerInterface { + const FAILURE_SUFFIX = 'ValidationFailed'; + /** * {@inheritdoc} */ @@ -21,10 +23,14 @@ public function handle(AbstractValidation $formValidation, MethodInvocation $inv unset($form); $args = (array) $invocation->getArguments(); $object = $invocation->getThis(); - if (! $formValidation instanceof FormValidation || ! method_exists($object, $formValidation->onFailure)) { + if (! $formValidation instanceof FormValidation) { + throw new InvalidOnFailureMethod(get_class($invocation->getThis())); + } + $onFailureMethod = $formValidation->onFailure ?: $invocation->getMethod()->getName() . self::FAILURE_SUFFIX; + if (! $formValidation instanceof FormValidation || ! method_exists($object, $onFailureMethod)) { throw new InvalidOnFailureMethod(get_class($invocation->getThis())); } - return call_user_func_array([$invocation->getThis(), $formValidation->onFailure], $args); + return call_user_func_array([$invocation->getThis(), $onFailureMethod], $args); } } diff --git a/tests/Fake/FakeController.php b/tests/Fake/FakeController.php index a2b2ac0..6ed7ae2 100644 --- a/tests/Fake/FakeController.php +++ b/tests/Fake/FakeController.php @@ -11,7 +11,7 @@ class FakeController /** * @var FormInterface */ - protected $form1; + protected $form; /** * @Inject @@ -19,18 +19,20 @@ class FakeController */ public function setForm(FormInterface $form) { - $this->form1 = $form; + $this->form = $form; } /** - * @FormValidation(form="form1", onFailure="badRequestAction") + * @FormValidation + * + * = is same as @ FormValidation(form="form", onFailure="createActionValidationFailed") */ public function createAction($name) { return '201'; } - public function badRequestAction() + public function createActionValidationFailed() { return '400'; }