Skip to content

Commit

Permalink
"ValidationFailed" method suffix
Browse files Browse the repository at this point in the history
for validation failed method
  • Loading branch information
koriym committed Sep 16, 2015
1 parent ba957db commit b06d3e0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 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

0 comments on commit b06d3e0

Please sign in to comment.