Skip to content

Commit

Permalink
Merge pull request schmittjoh#55 from merk/form-error-path
Browse files Browse the repository at this point in the history
Fix ability to add errors to forms by traversing the field
  • Loading branch information
schmittjoh committed Jul 6, 2012
2 parents 8924c8f + 5cb87db commit 5b6f3ba
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions Form/ChoosePaymentMethodType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

namespace JMS\Payment\CoreBundle\Form;

use JMS\Payment\CoreBundle\PluginController\Result;
use JMS\Payment\CoreBundle\Entity\PaymentInstruction;
use JMS\Payment\CoreBundle\Entity\ExtendedData;
use JMS\Payment\CoreBundle\Entity\PaymentInstruction;
use JMS\Payment\CoreBundle\PluginController\Result;
use JMS\Payment\CoreBundle\PluginController\PluginControllerInterface;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\CallbackValidator;
use JMS\Payment\CoreBundle\PluginController\PluginControllerInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\Util\PropertyPath;

/**
* Form Type for Choosing a Payment Method.
Expand Down Expand Up @@ -173,25 +174,31 @@ public function getName()

private function applyErrorsToForm(FormInterface $form, Result $result)
{
$ex = $result->getPluginException();
$ex = $result->getPluginException();

$globalErrors = $ex->getGlobalErrors();
$dataErrors = $ex->getDataErrors();
$globalErrors = $ex->getGlobalErrors();
$dataErrors = $ex->getDataErrors();

// add a generic error message
if (!$dataErrors && !$globalErrors) {
$form->addError(new FormError('form.error.invalid_payment_instruction'));
// add a generic error message
if (!$dataErrors && !$globalErrors) {
$form->addError(new FormError('form.error.invalid_payment_instruction'));

return;
}
return;
}

foreach ($globalErrors as $error) {
$form->addError(new FormError($error));
}
foreach ($globalErrors as $error) {
$form->addError(new FormError($error));
}

foreach ($dataErrors as $field => $error) {
$form->get($field)->addError(new FormError($error));
}
foreach ($dataErrors as $path => $error) {
$path = explode('.', $path);
$field = $form;
do {
$field = $field->get(array_shift($path));
} while ($path);

$field->addError(new FormError($error));
}
}

private function buildChoices(array $methods)
Expand All @@ -203,4 +210,4 @@ private function buildChoices(array $methods)

return $choices;
}
}
}

0 comments on commit 5b6f3ba

Please sign in to comment.