Merge branch 'master' into rewrite
This commit is contained in:
@@ -35,6 +35,7 @@ use Thelia\Core\Factory\ActionEventFactory;
|
||||
use Thelia\Form\BaseForm;
|
||||
use Thelia\Form\Exception\FormValidationException;
|
||||
use Symfony\Component\EventDispatcher\Event;
|
||||
use Thelia\Core\Event\DefaultActionEvent;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -60,10 +61,12 @@ class BaseController extends ContainerAware
|
||||
* Dispatch a Thelia event
|
||||
*
|
||||
* @param string $eventName a TheliaEvent name, as defined in TheliaEvents class
|
||||
* @param Event $event the event
|
||||
* @param Event $event the action event, or null (a DefaultActionEvent will be dispatched)
|
||||
*/
|
||||
protected function dispatch($eventName, Event $event = null)
|
||||
protected function dispatch($eventName, ActionEvent $event = null)
|
||||
{
|
||||
if ($event == null) $event = new DefaultActionEvent();
|
||||
|
||||
$this->getDispatcher()->dispatch($eventName, $event);
|
||||
}
|
||||
|
||||
@@ -117,6 +120,29 @@ class BaseController extends ContainerAware
|
||||
return $request->getSession();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all errors that occured in a form
|
||||
*
|
||||
* @param \Symfony\Component\Form\Form $form
|
||||
* @return string the error string
|
||||
*/
|
||||
private function getErrorMessages(\Symfony\Component\Form\Form $form) {
|
||||
|
||||
$errors = '';
|
||||
|
||||
foreach ($form->getErrors() as $key => $error) {
|
||||
$errors .= $error->getMessage() . ', ';
|
||||
}
|
||||
|
||||
foreach ($form->all() as $child) {
|
||||
if (!$child->isValid()) {
|
||||
$errors .= $this->getErrorMessages($child) . ', ';
|
||||
}
|
||||
}
|
||||
|
||||
return rtrim($errors, ', ');
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a BaseForm
|
||||
*
|
||||
@@ -135,17 +161,20 @@ class BaseController extends ContainerAware
|
||||
|
||||
if ($form->isValid()) {
|
||||
return $form;
|
||||
} else {
|
||||
throw new FormValidationException("Missing or invalid data");
|
||||
}
|
||||
} else {
|
||||
else {
|
||||
throw new FormValidationException(sprintf("Missing or invalid data: %s", $this->getErrorMessages($form)));
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw new FormValidationException(sprintf("Wrong form method, %s expected.", $expectedMethod));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* redirect request to specify url
|
||||
* redirect request to the specified url
|
||||
*
|
||||
* @param string $url
|
||||
*/
|
||||
public function redirect($url, $status = 302)
|
||||
@@ -154,12 +183,21 @@ class BaseController extends ContainerAware
|
||||
}
|
||||
|
||||
/**
|
||||
* If success_url param is present in request, follow this link.
|
||||
* If success_url param is present in request or in the provided form, redirect to this URL.
|
||||
*
|
||||
* @param BaseForm $form a base form, which may contains the success URL
|
||||
*/
|
||||
protected function redirectSuccess()
|
||||
protected function redirectSuccess(BaseForm $form = null)
|
||||
{
|
||||
if (null !== $url = $this->getRequest()->get("success_url")) {
|
||||
$this->redirect($url);
|
||||
if ($form != null) {
|
||||
$url = $form->getSuccessUrl();
|
||||
}
|
||||
else {
|
||||
$url = $this->getRequest()->get("success_url");
|
||||
}
|
||||
|
||||
echo "url=$url";
|
||||
|
||||
if (null !== $url) $this->redirect($url);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user