From b06d3e0eba0b358ffa5992e6a6c17a2cdfbc54c5 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Wed, 16 Sep 2015 11:59:14 +0900 Subject: [PATCH 1/2] "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'; } From 142eb882abc34366cbe82444038a2be6d5cfcf7a Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Wed, 16 Sep 2015 12:02:52 +0900 Subject: [PATCH 2/2] use default annotation method property --- tests/Fake/FakeController.php | 2 +- tests/Fake/FakeControllerVndError.php | 7 ++++--- tests/Fake/FakeInputValidationController.php | 6 +++--- tests/Fake/FakeInvalidController2.php | 2 +- tests/Fake/FakeInvalidInstanceController.php | 2 +- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/tests/Fake/FakeController.php b/tests/Fake/FakeController.php index 6ed7ae2..d642e52 100644 --- a/tests/Fake/FakeController.php +++ b/tests/Fake/FakeController.php @@ -24,7 +24,7 @@ public function setForm(FormInterface $form) /** * @FormValidation - * + * * = is same as @ FormValidation(form="form", onFailure="createActionValidationFailed") */ public function createAction($name) diff --git a/tests/Fake/FakeControllerVndError.php b/tests/Fake/FakeControllerVndError.php index e3cf8b9..3f44ab5 100644 --- a/tests/Fake/FakeControllerVndError.php +++ b/tests/Fake/FakeControllerVndError.php @@ -4,7 +4,7 @@ use Ray\Di\Di\Inject; use Ray\Di\Di\Named; -use Ray\WebFormModule\Annotation\FormValidation; +use Ray\WebFormModule\Annotation\InputValidation; use Ray\WebFormModule\Annotation\VndError; class FakeControllerVndError @@ -24,10 +24,11 @@ public function setForm(FormInterface $form) } /** - * @FormValidation(form="form1", onFailure="badRequestAction") + * @InputValidation(form="form1") * @VndError( * message="foo validation failed", - * logref="a1000", path="/path/to/error", + * logref="a1000", + * path="/path/to/error", * href={"_self"="/path/to/error", "help"="/path/to/help"} * ) */ diff --git a/tests/Fake/FakeInputValidationController.php b/tests/Fake/FakeInputValidationController.php index 0a8cefd..9619276 100644 --- a/tests/Fake/FakeInputValidationController.php +++ b/tests/Fake/FakeInputValidationController.php @@ -11,7 +11,7 @@ class FakeInputValidationController /** * @var FormInterface */ - protected $form1; + protected $form; /** * @Inject @@ -19,11 +19,11 @@ class FakeInputValidationController */ public function setForm(FormInterface $form) { - $this->form1 = $form; + $this->form = $form; } /** - * @InputValidation(form="form1") + * @InputValidation */ public function createAction($name) { diff --git a/tests/Fake/FakeInvalidController2.php b/tests/Fake/FakeInvalidController2.php index e17fa56..9f6bb47 100644 --- a/tests/Fake/FakeInvalidController2.php +++ b/tests/Fake/FakeInvalidController2.php @@ -9,7 +9,7 @@ class FakeInvalidController2 private $form = null; /** - * @FormValidation(form="form") + * @FormValidation */ public function createAction() { diff --git a/tests/Fake/FakeInvalidInstanceController.php b/tests/Fake/FakeInvalidInstanceController.php index 76aa509..d64b9c6 100644 --- a/tests/Fake/FakeInvalidInstanceController.php +++ b/tests/Fake/FakeInvalidInstanceController.php @@ -9,7 +9,7 @@ class FakeInvalidInstanceController private $form; /** - * @FormValidation(form="form") + * @FormValidation */ public function createAction() {