Skip to content

Commit

Permalink
Merge pull request #16 from koriym/on-failure-method
Browse files Browse the repository at this point in the history
On failure method suffix
  • Loading branch information
koriym committed Sep 16, 2015
2 parents ba957db + 142eb88 commit 1afcfd2
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 14 deletions.
10 changes: 8 additions & 2 deletions src/OnFailureMethodHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

final class OnFailureMethodHandler implements FailureHandlerInterface
{
const FAILURE_SUFFIX = 'ValidationFailed';

/**
* {@inheritdoc}
*/
Expand All @@ -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);
}
}
10 changes: 6 additions & 4 deletions tests/Fake/FakeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,28 @@ class FakeController
/**
* @var FormInterface
*/
protected $form1;
protected $form;

/**
* @Inject
* @Named("contact_form")
*/
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';
}
Expand Down
7 changes: 4 additions & 3 deletions tests/Fake/FakeControllerVndError.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"}
* )
*/
Expand Down
6 changes: 3 additions & 3 deletions tests/Fake/FakeInputValidationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ class FakeInputValidationController
/**
* @var FormInterface
*/
protected $form1;
protected $form;

/**
* @Inject
* @Named("contact_form")
*/
public function setForm(FormInterface $form)
{
$this->form1 = $form;
$this->form = $form;
}

/**
* @InputValidation(form="form1")
* @InputValidation
*/
public function createAction($name)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Fake/FakeInvalidController2.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class FakeInvalidController2
private $form = null;

/**
* @FormValidation(form="form")
* @FormValidation
*/
public function createAction()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Fake/FakeInvalidInstanceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class FakeInvalidInstanceController
private $form;

/**
* @FormValidation(form="form")
* @FormValidation
*/
public function createAction()
{
Expand Down

0 comments on commit 1afcfd2

Please sign in to comment.