refactor cartAdd EventListener, removing Request references

This commit is contained in:
Manuel Raynaud
2013-08-14 15:10:21 +02:00
parent 61342e06f4
commit ab81b58e94
8 changed files with 126 additions and 59 deletions

View File

@@ -32,6 +32,8 @@ use Thelia\Core\Template\ParserContext;
use Thelia\Core\Event\ActionEvent;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Thelia\Core\Factory\ActionEventFactory;
use Thelia\Form\BaseForm;
use Thelia\Action\Exception\FormValidationException;
/**
*
@@ -89,7 +91,7 @@ class BaseController extends ContainerAware
/**
* Return the event dispatcher,
*
* @return EventDispatcherInterface
* @return \Symfony\Component\EventDispatcher\EventDispatcher
*/
public function getDispatcher()
{
@@ -140,6 +142,32 @@ class BaseController extends ContainerAware
return $request->getSession();
}
/**
* Validate a BaseForm
*
* @param BaseForm $aBaseForm the form
* @param string $expectedMethod the expected method, POST or GET, or null for any of them
* @throws FormValidationException is the form contains error, or the method is not the right one
* @return \Symfony\Component\Form\Form Form the symfony form object
*/
protected function validateForm(BaseForm $aBaseForm, $expectedMethod = null)
{
$form = $aBaseForm->getForm();
if ($expectedMethod == null || $aBaseForm->getRequest()->isMethod($expectedMethod)) {
$form->bind($aBaseForm->getRequest());
if ($form->isValid()) {
return $form;
} else {
throw new FormValidationException("Missing or invalid data");
}
} else {
throw new FormValidationException(sprintf("Wrong form method, %s expected.", $expectedMethod));
}
}
/**
*
* redirect request to specify url