Form processing is now factorized in BaseAction

This commit is contained in:
franck
2013-08-08 11:13:46 +02:00
parent 13bed6d3fc
commit 4e83466bc0
7 changed files with 250 additions and 225 deletions

View File

@@ -23,11 +23,55 @@
namespace Thelia\Action;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Thelia\Form\CategoryDeletionForm;
use Thelia\Form\BaseForm;
use Thelia\Core\HttpFoundation\Request;
use Thelia\Action\Exception\FormValidationException;
use Thelia\Core\Event\ActionEvent;
abstract class BaseAction
{
protected function validateForm(BaseForm $aBaseForm, $expectedMethod = 'POST')
{
$form = $aBaseForm->getForm();
public function redirect($url, $status = 302)
if ($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));
}
}
/**
*
* @param BaseForm $aBaseForm
* @param string $error_message
* @param ActionEvent $event
*/
protected function propagateFormError(BaseForm $aBaseForm, $error_message, ActionEvent $event) {
// The form has an error
$aBaseForm->setError(true);
$aBaseForm->setErrorMessage($error_message);
// Store the form in the parser context
$event->setErrorForm($aBaseForm);
// Stop event propagation
$event->stopPropagation();
}
protected function redirect($url, $status = 302)
{
$response = new RedirectResponse($url, $status);

View File

@@ -33,28 +33,23 @@ use Thelia\Tools\Redirect;
use Thelia\Model\CategoryQuery;
use Thelia\Model\AdminLog;
use Thelia\Form\CategoryDeletionForm;
use Thelia\Action\Exception\FormValidationException;
class Category implements EventSubscriberInterface
class Category extends BaseAction implements EventSubscriberInterface
{
public function create(ActionEvent $event)
{
$request = $event->getRequest();
try {
$categoryCreationForm = new CategoryCreationForm($request);
$form = $categoryCreationForm->getForm();
if ($request->isMethod("post")) {
$form->bind($request);
if ($form->isValid()) {
$form = $this->validateForm($categoryCreationForm, "POST");
$data = $form->getData();
$category = new CategoryModel();
try {
$event->getDispatcher()->dispatch(TheliaEvents::BEFORE_CREATECATEGORY, $event);
$category->create(
@@ -75,29 +70,14 @@ class Category implements EventSubscriberInterface
// Redirect to the success URL
Redirect::exec($successUrl);
} catch (Exception $e) {
Tlog::getInstance()->error(sprintf('error during creating customer on action/createCustomer with message "%s"', $e->getMessage()));
} catch (PropelException $e) {
Tlog::getInstance()->error(sprintf('error during creating category with message "%s"', $e->getMessage()));
$message = "Failed to create this category, please try again.";
}
}
else {
$message = "Missing or invalid data";
}
}
else {
$message = "Wrong form method !";
}
// The form has an error
$categoryCreationForm->setError(true);
$categoryCreationForm->setErrorMessage($message);
// Store the form in the parser context
$event->setErrorForm($categoryCreationForm);
// Stop event propagation
$event->stopPropagation();
// The form has errors, propagate it.
$this->propagateFormError($categoryCreationForm, $message, $event);
}
public function modify(ActionEvent $event)
@@ -176,19 +156,13 @@ class Category implements EventSubscriberInterface
{
$request = $event->getRequest();
try {
$categoryDeletionForm = new CategoryDeletionForm($request);
$form = $categoryDeletionForm->getForm();
if ($request->isMethod("post")) {
$form->bind($request);
if ($form->isValid()) {
$form = $this->validateForm($categoryDeletionForm, "POST");
$data = $form->getData();
try {
$category = CategoryQuery::create()->findPk($data['id']);
$categoryEvent = new CategoryEvent($category);
@@ -215,24 +189,12 @@ class Category implements EventSubscriberInterface
$message = "Failed to change your account, please try again.";
}
}
else {
$message = "Missing or invalid data";
}
}
else {
$message = "Wrong form method !";
catch(FormValidationException $e) {
$message = $e->getMessage();
}
// The form has an error
$categoryDeletionForm->setError(true);
$categoryDeletionForm->setErrorMessage($message);
// Store the form in the parser context
$event->setErrorForm($categoryDeletionForm);
// Stop event propagation
$event->stopPropagation();
$this->propagateFormError($categoryDeletionForm, $message, $event);
}
/**

View File

@@ -41,6 +41,7 @@ use Symfony\Component\Validator\Exception\ValidatorException;
use Thelia\Core\Security\Exception\AuthenticationException;
use Thelia\Core\Security\Exception\UsernameNotFoundException;
use Propel\Runtime\Exception\PropelException;
use Thelia\Action\Exception\FormValidationException;
class Customer extends BaseAction implements EventSubscriberInterface
@@ -58,19 +59,14 @@ class Customer extends BaseAction implements EventSubscriberInterface
{
$request = $event->getRequest();
try {
$customerCreationForm = new CustomerCreation($request);
$form = $customerCreationForm->getForm();
$form = $this->validateForm($customerCreationForm, "POST");
if ($request->isMethod("post")) {
$form->bind($request);
if ($form->isValid()) {
$data = $form->getData();
$customer = new CustomerModel();
try {
$event->getDispatcher()->dispatch(TheliaEvents::BEFORE_CREATECUSTOMER, $event);
$customer->createOrUpdate(
@@ -88,54 +84,40 @@ class Customer extends BaseAction implements EventSubscriberInterface
$data["password"],
$request->getSession()->getLang()
);
$customerEvent = new CustomerEvent($customer);
$event->getDispatcher()->dispatch(TheliaEvents::AFTER_CREATECUSTOMER, $customerEvent);
// Connect the newly created user,and redirect to the success URL
$this->processSuccessfulLogin($event, $customer, $customerCreationForm, true);
} catch (PropelException $e) {
}
catch (PropelException $e) {
Tlog::getInstance()->error(sprintf('error during creating customer on action/createCustomer with message "%s"', $e->getMessage()));
$message = "Failed to create your account, please try again.";
}
}
else {
$message = "Missing or invalid data";
}
}
else {
$message = "Wrong form method !";
catch(FormValidationException $e) {
$message = $e->getMessage();
}
// The form has an error
$customerCreationForm->setError(true);
$customerCreationForm->setErrorMessage($message);
// Store the form in the parser context
$event->setErrorForm($customerCreationForm);
// Stop event propagation
$event->stopPropagation();
// The form has errors, propagate it.
$this->propagateFormError($customerCreationForm, $message, $event);
}
public function modify(ActionEvent $event)
{
$request = $event->getRequest();
try {
$customerModification = new CustomerModification($request);
$form = $customerModification->getForm();
$form = $this->validateForm($customerModification, "POST");
if ($request->isMethod("post")) {
$form->bind($request);
if ($form->isValid()) {
$data = $form->getData();
$customer = CustomerQuery::create()->findPk(1);
try {
$customerEvent = new CustomerEvent($customer);
$event->getDispatcher()->dispatch(TheliaEvents::BEFORE_CHANGECUSTOMER, $customerEvent);
@@ -167,21 +149,13 @@ class Customer extends BaseAction implements EventSubscriberInterface
$message = "Failed to change your account, please try again.";
}
}
else {
$message = "Missing or invalid data";
}
}
else {
$message = "Wrong form method !";
catch(FormValidationException $e) {
$message = $e->getMessage();
}
// The form has an error
$customerModification->setError(true);
$customerModification->setErrorMessage($message);
// Dispatch the errored form
$event->setErrorForm($customerModification);
// The form has errors, propagate it.
$this->propagateFormError($customerModification, $message, $event);
}

View File

@@ -0,0 +1,29 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Action\Exception;
class FormValidationException extends ActionException
{
}

View File

@@ -154,7 +154,7 @@ class BaseAdminController extends ContainerAware
$actionEvent = $eventFactory->createActionEvent();
$this->getDispatcher()->dispatch("action.$action", $actionEvent);
$this->dispatch("action.$action", $actionEvent);
if ($actionEvent->hasErrorForm()) {
$this->getParserContext()->setErrorForm($actionEvent->getErrorForm());
@@ -163,6 +163,17 @@ class BaseAdminController extends ContainerAware
return $actionEvent;
}
/**
* Dispatch a Thelia event to modules
*
* @param string $eventName a TheliaEvent name, as defined in TheliaEvents class
* @param ActionEvent $event the event
*/
protected function dispatch($eventName, ActionEvent $event = null) {
$this->getDispatcher()->dispatch($eventName, $event);
}
/**
* Return the event dispatcher,
*

View File

@@ -33,14 +33,11 @@
<form name="thelia.customer.login" class="Thelia\Form\CustomerLogin"/>
<form name="thelia.admin.login" class="Thelia\Form\AdminLogin"/>
<<<<<<< HEAD
<form name="thelia.admin.category.creation" class="Thelia\Form\CategoryCreationForm"/>
<form name="thelia.admin.category.deletion" class="Thelia\Form\CategoryDeletionForm"/>
=======
<form name="thelia.cart.add" class="Thelia\Form\CartAdd"/>
>>>>>>> cart
</forms>

View File

@@ -44,6 +44,8 @@ abstract class BaseForm {
*/
protected $form;
protected $request;
private $view = null;
/**
@@ -60,6 +62,8 @@ abstract class BaseForm {
public function __construct(Request $request, $type= "form", $data = array(), $options = array())
{
$this->request = $request;
$validator = Validation::createValidator();
if(!isset($options["attr"]["name"])) {
@@ -94,6 +98,10 @@ abstract class BaseForm {
$this->form = $this->formBuilder->getForm();
}
public function getRequest() {
return $this->request;
}
protected function cleanOptions($options)
{
unset($options["csrf_protection"]);