An errorred form is now passed to the Form smarty plugin through the parser context instead of the

session
This commit is contained in:
franck
2013-07-24 17:31:20 +02:00
parent 7498c97f49
commit fabeab1822
13 changed files with 160 additions and 94 deletions

View File

@@ -79,33 +79,4 @@ abstract class ActionEvent extends Event
{
return $this->request;
}
/**
* Store a form taht contains error, to pass it to the current session.
*
* @param BaseForm $form
*/
public function setFormError(BaseForm $form)
{
$this->form = $form;
$this->stopPropagation();
}
/**
* @return BaseForm the errored form, or null
*/
public function getFormError()
{
return $this->form;
}
/**
* Check if theis event contains a form with errors
*
* @return boolean
*/
public function hasFormError()
{
return $this->form !== null;
}
}

View File

@@ -49,10 +49,6 @@ class ControllerListener implements EventSubscriberInterface
$event = new ActionEventFactory($request, $action, $event->getKernel()->getContainer()->getParameter("thelia.actionEvent"));
$actionEvent = $event->createActionEvent();
$dispatcher->dispatch("action.".$action, $actionEvent);
if ($actionEvent->hasFormError()) {
$request->getSession()->setErrorFormName($actionEvent->getFormError()->getName());
}
}
}

View File

@@ -77,26 +77,6 @@ class Session extends BaseSession {
return $this->remove('admin_user');
}
// -- Error form -----------------------------------------------------------
/**
* @param string $formName the form name
*/
public function setErrorFormName($formName)
{
$this->set('error_form', $formName);
}
public function getErrorFormName()
{
return $this->get('error_form', null);
}
public function clearErrorFormName()
{
return $this->remove('error_form');
}
// -- Return page ----------------------------------------------------------
public function setReturnToUrl($url)

View File

@@ -26,6 +26,7 @@ namespace Thelia\Core\Template;
use Thelia\Model\ConfigQuery;
use Thelia\Core\HttpFoundation\Request;
use Thelia\Tools\URL;
use Thelia\Form\BaseForm;
/**
* The parser context is an application-wide context, which stores var-value pairs.
* Theses pairs are injected in the parser and becomes available to the templates.
@@ -46,6 +47,28 @@ class ParserContext implements \IteratorAggregate
;
}
// -- Error form -----------------------------------------------------------
/**
* @param BaseForm $form the errored form
*/
public function setErrorForm(BaseForm $form)
{
$this->set('error_form', $form);
}
public function getErrorForm()
{
return $this->get('error_form', null);
}
public function clearErrorForm()
{
return $this->remove('error_form');
}
// -- Internal table manipulation ------------------------------------------
public function set($name, $value)
{
$this->store[$name] = $value;
@@ -53,9 +76,16 @@ class ParserContext implements \IteratorAggregate
return $this;
}
public function get($name)
public function remove($name)
{
return $this->store[$name];
unset($this->store[$name]);
return $this;
}
public function get($name, $default = null)
{
return isset($this->store[$name]) ? $this->store[$name] : $default;
}
public function getIterator()

View File

@@ -29,6 +29,7 @@ use Symfony\Component\HttpFoundation\Request;
use Thelia\Core\Template\Smarty\SmartyPluginDescriptor;
use Thelia\Core\Template\Smarty\SmartyPluginInterface;
use Thelia\Log\Tlog;
use Thelia\Core\Template\ParserContext;
/**
*
@@ -58,12 +59,14 @@ class Form implements SmartyPluginInterface
{
protected $request;
protected $parserContext;
protected $formDefinition = array();
public function __construct(Request $request)
public function __construct(Request $request, ParserContext $parserContext)
{
$this->request = $request;
$this->parserContext = $parserContext;
}
public function setFormDefinition($formDefinition)
@@ -88,21 +91,23 @@ class Form implements SmartyPluginInterface
$instance = $this->createInstance($params['name']);
// Check if session contains our form
$errorForm = $this->request->getSession()->getErrorFormName();
// Check if parser context contains our form
$errorForm = $this->parserContext->getErrorForm();
if ($errorForm == $instance->getName()) {
if (null != $errorForm && $errorForm->getName() == $instance->getName()) {
// Bind form with current request to get error messages and field values.
$instance->getForm()->bind($this->request);
// Re-use the errored form
$instance = $errorForm;
// Remove the form from the session
$this->request->getSession()->clearErrorFormName();
$this->parserContext->clearErrorForm();
}
$instance->createView();
$template->assign("form", $instance);
$template->assign("form_error", $instance->hasError() ? true : false);
$template->assign("form_error_message", $instance->getErrorMessage());
}
else {
return $content;