From 995e1eb679954c64d106e82a1ee27a33f3cb8f3e Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Fri, 28 Jun 2013 11:36:35 +0200 Subject: [PATCH 01/10] refactor how to create Form --- core/lib/Thelia/Action/Customer.php | 17 ++++++++++ .../Core/Template/Smarty/Plugins/Form.php | 32 ++++++++++++++++--- core/lib/Thelia/Form/BaseForm.php | 31 ++++++++++++------ core/lib/Thelia/Form/CustomerCreation.php | 22 +++++-------- templates/smarty-sample/index.html | 3 +- 5 files changed, 76 insertions(+), 29 deletions(-) diff --git a/core/lib/Thelia/Action/Customer.php b/core/lib/Thelia/Action/Customer.php index 48c2a2e0e..186b8e34c 100755 --- a/core/lib/Thelia/Action/Customer.php +++ b/core/lib/Thelia/Action/Customer.php @@ -25,13 +25,30 @@ namespace Thelia\Action; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Thelia\Core\Event\ActionEvent; +use Thelia\Form\BaseForm; +use Thelia\Form\CustomerCreation; class Customer implements EventSubscriberInterface { public function create(ActionEvent $event) { + $request = $event->getRequest(); + $customerForm = new CustomerCreation($request); + + $form = $customerForm->getForm(); + + + if ($request->isMethod("post")) { + $form->bind($request); + + if ($form->isValid()) { + echo "ok"; exit; + } else { + echo "ko"; exit; + } + } } public function modify(ActionEvent $event) diff --git a/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php b/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php index d1ef26244..51ff06e9c 100644 --- a/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php +++ b/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php @@ -30,6 +30,30 @@ use Thelia\Core\Template\Smarty\SmartyPluginDescriptor; use Thelia\Core\Template\Smarty\SmartyPluginInterface; use Thelia\Log\Tlog; +/** + * + * Plugin for smarty defining blocks and functions for using Form display. + * + * blocks : + * - {form name="myForm"} ... {/form} => find form named myForm, + * create an instance and assign this instanciation into smarty variable. Form must be declare into + * config using tag + * + * - {form_field form=$form.fieldName} {/form_field} This block find info into the Form field containing by + * the form paramter. This field must be an instance of FormView. fieldName is the name of your field. This block + * can output these info : + * * $name => name of yout input + * * $value => value for your input + * * $label => label for your input + * * $error => boolean for know if there is error for this field + * * $attr => all your attribute for your input (define when you construct programmatically you form) + * + * - {form_error form=$form.fieldName} ... {/form_error} Display this block if there are errors on this field. + * fieldName is the name of your field + * + * Class Form + * @package Thelia\Core\Template\Smarty\Plugins + */ class Form implements SmartyPluginInterface { @@ -63,11 +87,7 @@ class Form implements SmartyPluginInterface throw new \InvalidArgumentException("Missing 'name' parameter in form arguments"); } - $form = BaseForm::getFormFactory($this->request); - $formBuilder = $form->createBuilder('form'); - $instance = $this->getInstance($params['name']); - $instance = $instance->buildForm($formBuilder, array()); $template->assign("form", $instance->getForm()->createView()); } else { @@ -179,8 +199,10 @@ class Form implements SmartyPluginInterface throw new ElementNotFoundException(sprintf("%s form does not exists", $name)); } + $class = new \ReflectionClass($this->formDefinition[$name]); - return new $this->formDefinition[$name]; + + return $class->newInstance($this->request); } /** diff --git a/core/lib/Thelia/Form/BaseForm.php b/core/lib/Thelia/Form/BaseForm.php index da6689f24..02f86fe02 100644 --- a/core/lib/Thelia/Form/BaseForm.php +++ b/core/lib/Thelia/Form/BaseForm.php @@ -32,30 +32,43 @@ use Symfony\Component\Form\Extension\Csrf\CsrfProvider\SessionCsrfProvider; use Symfony\Component\Validator\Validation; use Thelia\Model\ConfigQuery; -class BaseForm { - +abstract class BaseForm { /** - * @param Request $request - * @return \Symfony\Component\Form\FormFactoryInterface + * @var \Symfony\Component\Form\FormFactoryInterface */ - public static function getFormFactory(Request $request, $secret = null) + protected $form; + + public function __construct(Request $request, $type= "form", $data = array(), $options = array()) { $validator = Validation::createValidator(); - $form = Forms::createFormFactoryBuilder() + $this->form = Forms::createFormFactoryBuilder() ->addExtension(new HttpFoundationExtension()) ->addExtension( new CsrfExtension( new SessionCsrfProvider( $request->getSession(), - $secret ?: ConfigQuery::read("form.secret", md5(__DIR__)) + isset($option["secret"]) ? $option["secret"] : ConfigQuery::read("form.secret", md5(__DIR__)) ) ) ) ->addExtension(new ValidatorExtension($validator)) - ->getFormFactory(); + ->getFormFactory() + ->createBuilder($type, $data, $options); + ; - return $form; + $this->buildForm(); } + + /** + * @return \Symfony\Component\Form\Form + */ + public function getForm() + { + return $this->form->getForm(); + } + + abstract protected function buildForm(); + } diff --git a/core/lib/Thelia/Form/CustomerCreation.php b/core/lib/Thelia/Form/CustomerCreation.php index 120be2f41..3906be565 100644 --- a/core/lib/Thelia/Form/CustomerCreation.php +++ b/core/lib/Thelia/Form/CustomerCreation.php @@ -24,31 +24,25 @@ namespace Thelia\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Validator\Constraints\NotBlank; -class CustomerCreation extends AbstractType +class CustomerCreation extends BaseForm { - public function buildForm(FormBuilderInterface $builder, array $options) + public function buildForm() { - return $builder->add("name", "text") + $this->form->add("name", "text") ->add("email", "email", array( "attr" => array( "class" => "field" ), - "label" => "email" + "label" => "email", + "constraints" => array( + new NotBlank() + ) ) ) ->add('age', 'integer'); } - - /** - * Returns the name of this type. - * - * @return string The name of this type - */ - public function getName() - { - return "customer creation"; - } } \ No newline at end of file diff --git a/templates/smarty-sample/index.html b/templates/smarty-sample/index.html index 2332aa5ff..174505335 100755 --- a/templates/smarty-sample/index.html +++ b/templates/smarty-sample/index.html @@ -8,7 +8,7 @@ An image from asset directory : {intl l='An internationalized string'} {form name="thelia.customer.creation"} -
+ {form_field_hidden form=$form} {form_field form=$form.email} @@ -21,6 +21,7 @@ An image from asset directory : {form_field form=$form.age} {intl l='age'} : {/form_field} +
{/form}
From 6809c5ba1c76d32ef9a3fc5b0a1dee5b26b2c088 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Fri, 28 Jun 2013 11:45:54 +0200 Subject: [PATCH 02/10] reimplement form already created --- .../Thelia/Admin/Controller/AdminController.php | 6 ++---- core/lib/Thelia/Form/AdminLogin.php | 16 +++------------- 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/core/lib/Thelia/Admin/Controller/AdminController.php b/core/lib/Thelia/Admin/Controller/AdminController.php index bb5536c7e..91b6123b8 100755 --- a/core/lib/Thelia/Admin/Controller/AdminController.php +++ b/core/lib/Thelia/Admin/Controller/AdminController.php @@ -50,11 +50,9 @@ class AdminController extends BaseAdminController { protected function getLoginForm() { - $form = $this->getFormBuilder(); + $adminLogin = new AdminLogin($this->getRequest()); - $adminLogin = new AdminLogin(); - - return $adminLogin->buildForm($form, array())->getForm(); + return $adminLogin->getForm(); } public function lostAction() diff --git a/core/lib/Thelia/Form/AdminLogin.php b/core/lib/Thelia/Form/AdminLogin.php index 2bd2debac..2fa2c8d5f 100644 --- a/core/lib/Thelia/Form/AdminLogin.php +++ b/core/lib/Thelia/Form/AdminLogin.php @@ -23,16 +23,15 @@ namespace Thelia\Form; -use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Validator\Constraints\Length; use Symfony\Component\Validator\Constraints\NotBlank; -class AdminLogin extends AbstractType { +class AdminLogin extends BaseForm { - public function buildForm(FormBuilderInterface $builder, array $options) + protected function buildForm() { - return $builder + $this->form ->add("username", "text", array( "constraints" => array( new NotBlank(), @@ -42,13 +41,4 @@ class AdminLogin extends AbstractType { ->add("password", "password"); } - /** - * Returns the name of this type. - * - * @return string The name of this type - */ - public function getName() - { - return "admin_login"; - } } \ No newline at end of file From a14d3da90acca2e1b28f02d4ee71979713a080b1 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Fri, 28 Jun 2013 11:47:28 +0200 Subject: [PATCH 03/10] ichange CustomerCreation::buildForm visibility --- core/lib/Thelia/Form/CustomerCreation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/lib/Thelia/Form/CustomerCreation.php b/core/lib/Thelia/Form/CustomerCreation.php index 3906be565..68726356d 100644 --- a/core/lib/Thelia/Form/CustomerCreation.php +++ b/core/lib/Thelia/Form/CustomerCreation.php @@ -30,7 +30,7 @@ use Symfony\Component\Validator\Constraints\NotBlank; class CustomerCreation extends BaseForm { - public function buildForm() + protected function buildForm() { $this->form->add("name", "text") ->add("email", "email", array( From 3b5b29c6e0db27b9d0d5d95892807cdafae2c8d3 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Mon, 1 Jul 2013 11:10:52 +0200 Subject: [PATCH 04/10] retrieve form error in front office --- core/lib/Thelia/Action/Customer.php | 2 +- .../Admin/Controller/BaseAdminController.php | 12 ------------ core/lib/Thelia/Core/Event/ActionEvent.php | 19 +++++++++++++++++++ .../Core/EventListener/ControllerListener.php | 9 ++++++++- .../Core/Template/Smarty/Plugins/Form.php | 13 +++++++++++-- core/lib/Thelia/Form/BaseForm.php | 13 +++++++++++-- core/lib/Thelia/Form/CustomerCreation.php | 5 +++++ templates/smarty-sample/index.html | 3 +++ 8 files changed, 58 insertions(+), 18 deletions(-) diff --git a/core/lib/Thelia/Action/Customer.php b/core/lib/Thelia/Action/Customer.php index 186b8e34c..a794035e2 100755 --- a/core/lib/Thelia/Action/Customer.php +++ b/core/lib/Thelia/Action/Customer.php @@ -46,7 +46,7 @@ class Customer implements EventSubscriberInterface if ($form->isValid()) { echo "ok"; exit; } else { - echo "ko"; exit; + $event->setFormError($form); } } } diff --git a/core/lib/Thelia/Admin/Controller/BaseAdminController.php b/core/lib/Thelia/Admin/Controller/BaseAdminController.php index 3aa0e9281..1c193a966 100755 --- a/core/lib/Thelia/Admin/Controller/BaseAdminController.php +++ b/core/lib/Thelia/Admin/Controller/BaseAdminController.php @@ -79,16 +79,4 @@ class BaseAdminController extends ContainerAware return $parser; } - - public function getFormFactory() - { - return BaseForm::getFormFactory($this->getRequest(), ConfigQuery::read("form.secret.admin", md5(__DIR__))); - } - - public function getFormBuilder() - { - return $this->getFormFactory()->createBuilder("form"); - } - - } \ No newline at end of file diff --git a/core/lib/Thelia/Core/Event/ActionEvent.php b/core/lib/Thelia/Core/Event/ActionEvent.php index b2d1d7b77..32074c559 100755 --- a/core/lib/Thelia/Core/Event/ActionEvent.php +++ b/core/lib/Thelia/Core/Event/ActionEvent.php @@ -47,6 +47,8 @@ abstract class ActionEvent extends Event */ protected $action; + protected $form; + /** * * @param \Symfony\Component\HttpFoundation\Request $request @@ -75,4 +77,21 @@ abstract class ActionEvent extends Event { return $this->request; } + + public function setFormError($form) + { + $this->form = $form; + $this->stopPropagation(); + } + + public function getForm() + { + return $this->form; + } + + public function hasFormError() + { + return $this->form !== null; + } + } diff --git a/core/lib/Thelia/Core/EventListener/ControllerListener.php b/core/lib/Thelia/Core/EventListener/ControllerListener.php index 607c2a44d..93c827c09 100755 --- a/core/lib/Thelia/Core/EventListener/ControllerListener.php +++ b/core/lib/Thelia/Core/EventListener/ControllerListener.php @@ -46,7 +46,14 @@ class ControllerListener implements EventSubscriberInterface if (false !== $action = $request->get("action")) { //search corresponding action $event = new ActionEventFactory($request, $action, $event->getKernel()->getContainer()->getParameter("thelia.actionEvent")); - $dispatcher->dispatch("action.".$action, $event->createActionEvent()); + $actionEvent = $event->createActionEvent(); + $dispatcher->dispatch("action.".$action, $actionEvent); + + if ($actionEvent->hasFormError()) { + $request->getSession()->set("form_error", true); + $request->getSession()->set("form_name", $actionEvent->getForm()->createView() + ->vars["attr"]["thelia_name"]); + } } } diff --git a/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php b/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php index 51ff06e9c..0870ba2c2 100644 --- a/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php +++ b/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php @@ -88,8 +88,14 @@ class Form implements SmartyPluginInterface } $instance = $this->getInstance($params['name']); + $form = $instance->getForm(); - $template->assign("form", $instance->getForm()->createView()); + if (true === $this->request->getSession()->get("form_error", false) && $this->request->getSession()->get + ("form_name") == $instance->getName()) { + $form->bind($this->request); + } + + $template->assign("form", $form->createView()); } else { return $content; } @@ -202,7 +208,10 @@ class Form implements SmartyPluginInterface $class = new \ReflectionClass($this->formDefinition[$name]); - return $class->newInstance($this->request); + return $class->newInstance( + $this->request, + "form" + ); } /** diff --git a/core/lib/Thelia/Form/BaseForm.php b/core/lib/Thelia/Form/BaseForm.php index 02f86fe02..cbcebf173 100644 --- a/core/lib/Thelia/Form/BaseForm.php +++ b/core/lib/Thelia/Form/BaseForm.php @@ -30,6 +30,7 @@ use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension; use Symfony\Component\Form\Extension\Csrf\CsrfExtension; use Symfony\Component\Form\Extension\Csrf\CsrfProvider\SessionCsrfProvider; use Symfony\Component\Validator\Validation; +use Thelia\Form\Extension\NameFormExtension; use Thelia\Model\ConfigQuery; abstract class BaseForm { @@ -38,17 +39,23 @@ abstract class BaseForm { */ protected $form; + public $name; + public function __construct(Request $request, $type= "form", $data = array(), $options = array()) { $validator = Validation::createValidator(); + if(!isset($options["attr"]["name"])) { + $options["attr"]["thelia_name"] = $this->getName(); + } + $this->form = Forms::createFormFactoryBuilder() ->addExtension(new HttpFoundationExtension()) ->addExtension( new CsrfExtension( new SessionCsrfProvider( $request->getSession(), - isset($option["secret"]) ? $option["secret"] : ConfigQuery::read("form.secret", md5(__DIR__)) + isset($options["secret"]) ? $options["secret"] : ConfigQuery::read("form.secret", md5(__DIR__)) ) ) ) @@ -57,6 +64,8 @@ abstract class BaseForm { ->createBuilder($type, $data, $options); ; + + $this->buildForm(); } @@ -69,6 +78,6 @@ abstract class BaseForm { } abstract protected function buildForm(); - + abstract public function getName(); } diff --git a/core/lib/Thelia/Form/CustomerCreation.php b/core/lib/Thelia/Form/CustomerCreation.php index 68726356d..bcfacbf8a 100644 --- a/core/lib/Thelia/Form/CustomerCreation.php +++ b/core/lib/Thelia/Form/CustomerCreation.php @@ -45,4 +45,9 @@ class CustomerCreation extends BaseForm ) ->add('age', 'integer'); } + + public function getName() + { + return "customerCreation"; + } } \ No newline at end of file diff --git a/templates/smarty-sample/index.html b/templates/smarty-sample/index.html index 174505335..25814172e 100755 --- a/templates/smarty-sample/index.html +++ b/templates/smarty-sample/index.html @@ -12,6 +12,9 @@ An image from asset directory : {form_field_hidden form=$form} {form_field form=$form.email} + {form_error form=$form.email} + {$message} + {/form_error} {intl l="{$label}"} : {/form_field} From 0dc10d509744c624eb79377dc37994c81fa10ed1 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Mon, 1 Jul 2013 11:20:40 +0200 Subject: [PATCH 05/10] add some phpdoc for form creation explanation --- core/lib/Thelia/Form/BaseForm.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/core/lib/Thelia/Form/BaseForm.php b/core/lib/Thelia/Form/BaseForm.php index cbcebf173..3c76df1b5 100644 --- a/core/lib/Thelia/Form/BaseForm.php +++ b/core/lib/Thelia/Form/BaseForm.php @@ -77,7 +77,31 @@ abstract class BaseForm { return $this->form->getForm(); } + /** + * + * in this function you add all the fields you need for your Form. + * Form this you have to call add method on $this->form attribute : + * + * $this->form->add("name", "text") + * ->add("email", "email", array( + * "attr" => array( + * "class" => "field" + * ), + * "label" => "email", + * "constraints" => array( + * new NotBlank() + * ) + * ) + * ) + * ->add('age', 'integer'); + * + * @return null + */ abstract protected function buildForm(); + + /** + * @return string the name of you form. This name must be unique + */ abstract public function getName(); } From 26be670f5724494bd064605ee5771a22a6e1b813 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Mon, 1 Jul 2013 11:22:05 +0200 Subject: [PATCH 06/10] reset form session after binding errors --- core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php | 1 + 1 file changed, 1 insertion(+) diff --git a/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php b/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php index 0870ba2c2..f7ee96d2a 100644 --- a/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php +++ b/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php @@ -93,6 +93,7 @@ class Form implements SmartyPluginInterface if (true === $this->request->getSession()->get("form_error", false) && $this->request->getSession()->get ("form_name") == $instance->getName()) { $form->bind($this->request); + $this->request->getSession()->set("form_error", false); } $template->assign("form", $form->createView()); From 762b5bc71c3aded1bc7ad0f74f26f618fb06f477 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Mon, 1 Jul 2013 11:54:40 +0200 Subject: [PATCH 07/10] name all form field --- core/lib/Thelia/Config/Resources/config.xml | 1 + .../Thelia/Core/Template/Smarty/Plugins/Form.php | 6 ++++-- core/lib/Thelia/Form/AdminLogin.php | 5 +++++ core/lib/Thelia/Form/BaseForm.php | 2 +- templates/smarty-sample/index.html | 15 +++++++++++++++ 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/core/lib/Thelia/Config/Resources/config.xml b/core/lib/Thelia/Config/Resources/config.xml index d1e6569fa..4eb0217ae 100755 --- a/core/lib/Thelia/Config/Resources/config.xml +++ b/core/lib/Thelia/Config/Resources/config.xml @@ -23,6 +23,7 @@
+ diff --git a/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php b/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php index f7ee96d2a..2ff6be8e3 100644 --- a/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php +++ b/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php @@ -90,8 +90,10 @@ class Form implements SmartyPluginInterface $instance = $this->getInstance($params['name']); $form = $instance->getForm(); - if (true === $this->request->getSession()->get("form_error", false) && $this->request->getSession()->get - ("form_name") == $instance->getName()) { + if ( + true === $this->request->getSession()->get("form_error", false) && + $this->request->getSession()->get("form_name") == $instance->getName()) + { $form->bind($this->request); $this->request->getSession()->set("form_error", false); } diff --git a/core/lib/Thelia/Form/AdminLogin.php b/core/lib/Thelia/Form/AdminLogin.php index 2fa2c8d5f..33810f771 100644 --- a/core/lib/Thelia/Form/AdminLogin.php +++ b/core/lib/Thelia/Form/AdminLogin.php @@ -41,4 +41,9 @@ class AdminLogin extends BaseForm { ->add("password", "password"); } + public function getName() + { + return "admin_login"; + } + } \ No newline at end of file diff --git a/core/lib/Thelia/Form/BaseForm.php b/core/lib/Thelia/Form/BaseForm.php index 3c76df1b5..3a6d69cb3 100644 --- a/core/lib/Thelia/Form/BaseForm.php +++ b/core/lib/Thelia/Form/BaseForm.php @@ -61,7 +61,7 @@ abstract class BaseForm { ) ->addExtension(new ValidatorExtension($validator)) ->getFormFactory() - ->createBuilder($type, $data, $options); + ->createNamedBuilder($this->getName(), $type, $data, $options); ; diff --git a/templates/smarty-sample/index.html b/templates/smarty-sample/index.html index 25814172e..28f78f06c 100755 --- a/templates/smarty-sample/index.html +++ b/templates/smarty-sample/index.html @@ -27,6 +27,21 @@ An image from asset directory :
{/form} + +
+ +{form name="thelia.admin_login"} +
+ {form_field_hidden form=$form} + {form_field form=$form.username} + username : + {/form_field} + + {form_field form=$form.password} + password : + {/form_field} +
+{/form}
jQuery data:
From 6e292944180274b02d5b33502a5665adc423ff5e Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Mon, 1 Jul 2013 15:39:58 +0200 Subject: [PATCH 08/10] update create customer form --- core/lib/Thelia/Action/Customer.php | 6 +++ core/lib/Thelia/Core/Event/TheliaEvents.php | 4 ++ core/lib/Thelia/Form/CustomerCreation.php | 54 ++++++++++++++++----- 3 files changed, 52 insertions(+), 12 deletions(-) diff --git a/core/lib/Thelia/Action/Customer.php b/core/lib/Thelia/Action/Customer.php index a794035e2..7a26d40b6 100755 --- a/core/lib/Thelia/Action/Customer.php +++ b/core/lib/Thelia/Action/Customer.php @@ -25,6 +25,7 @@ namespace Thelia\Action; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Thelia\Core\Event\ActionEvent; +use Thelia\Core\Event\TheliaEvents; use Thelia\Form\BaseForm; use Thelia\Form\CustomerCreation; @@ -33,6 +34,9 @@ class Customer implements EventSubscriberInterface public function create(ActionEvent $event) { + + $event->getDispatcher()->dispatch(TheliaEvents::BEFORE_CREATECUSTOMER, $event); + $request = $event->getRequest(); $customerForm = new CustomerCreation($request); @@ -49,6 +53,8 @@ class Customer implements EventSubscriberInterface $event->setFormError($form); } } + + $event->getDispatcher()->dispatch(TheliaEvents::AFTER_CREATECUSTOMER, $event); } public function modify(ActionEvent $event) diff --git a/core/lib/Thelia/Core/Event/TheliaEvents.php b/core/lib/Thelia/Core/Event/TheliaEvents.php index 3ffac068f..8f44d7fb5 100755 --- a/core/lib/Thelia/Core/Event/TheliaEvents.php +++ b/core/lib/Thelia/Core/Event/TheliaEvents.php @@ -47,4 +47,8 @@ final class TheliaEvents * Send before starting thelia inclusion */ const INCLUSION = "thelia.include"; + + const BEFORE_CREATECUSTOMER = "action.before_createcustomer"; + + const AFTER_CREATECUSTOMER = "action.after_createcustomer"; } diff --git a/core/lib/Thelia/Form/CustomerCreation.php b/core/lib/Thelia/Form/CustomerCreation.php index bcfacbf8a..a925d3d56 100644 --- a/core/lib/Thelia/Form/CustomerCreation.php +++ b/core/lib/Thelia/Form/CustomerCreation.php @@ -24,7 +24,8 @@ namespace Thelia\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; -use Symfony\Component\Validator\Constraints\NotBlank; +use Symfony\Component\Validator\Constraints; +use Thelia\Model\ConfigQuery; class CustomerCreation extends BaseForm @@ -32,18 +33,47 @@ class CustomerCreation extends BaseForm protected function buildForm() { - $this->form->add("name", "text") - ->add("email", "email", array( - "attr" => array( - "class" => "field" - ), - "label" => "email", - "constraints" => array( - new NotBlank() - ) + $this->form + ->add("firstname", "text", array( + "constraints" => array( + new Constraints\NotBlank() ) - ) - ->add('age', 'integer'); + )) + ->add("lastname", "text", array( + "constraints" => array( + new Constraints\NotBlank() + ) + )) + ->add("email", "email", array( + "constraints" => array( + new Constraints\NotBlank(), + new Constraints\Email() + ) + )) + ->add("address1", "text", array( + "constraints" => array( + new Constraints\NotBlank() + ) + )) + ->add("address2", "text") + ->add("address3", "text") + ->add("zipcode", "text", array( + "constraints" => array( + new Constraints\NotBlank() + ) + )) + ->add("country", "text", array( + "constraints" => array( + new Constraints\NotBlank() + ) + )) + ->add("password", "password", array( + "constraints" => array( + new Constraints\Length(array("min" => ConfigQuery::read("password.length", 4))) + ) + )) + + ; } public function getName() From 62a3dc3074560f18ee358a9cd57dfc3c7030159f Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Mon, 1 Jul 2013 16:16:24 +0200 Subject: [PATCH 09/10] create creation account form --- core/lib/Thelia/Form/CustomerCreation.php | 20 +++++++++----- templates/smarty-sample/connexion.html | 27 +++++++++++++++++++ templates/smarty-sample/index.html | 33 ----------------------- 3 files changed, 41 insertions(+), 39 deletions(-) create mode 100644 templates/smarty-sample/connexion.html diff --git a/core/lib/Thelia/Form/CustomerCreation.php b/core/lib/Thelia/Form/CustomerCreation.php index a925d3d56..80efb74da 100644 --- a/core/lib/Thelia/Form/CustomerCreation.php +++ b/core/lib/Thelia/Form/CustomerCreation.php @@ -37,26 +37,34 @@ class CustomerCreation extends BaseForm ->add("firstname", "text", array( "constraints" => array( new Constraints\NotBlank() - ) + ), + "label" => "firstname" )) ->add("lastname", "text", array( "constraints" => array( new Constraints\NotBlank() - ) + ), + "label" => "lastname" )) ->add("email", "email", array( "constraints" => array( new Constraints\NotBlank(), new Constraints\Email() - ) + ), + "label" => "email" )) ->add("address1", "text", array( "constraints" => array( new Constraints\NotBlank() - ) + ), + "label" => "address" + )) + ->add("address2", "text", array( + "label" => "Address Line 2" + )) + ->add("address3", "text", array( + "label" => "Address Line 3" )) - ->add("address2", "text") - ->add("address3", "text") ->add("zipcode", "text", array( "constraints" => array( new Constraints\NotBlank() diff --git a/templates/smarty-sample/connexion.html b/templates/smarty-sample/connexion.html new file mode 100644 index 000000000..69c4b516c --- /dev/null +++ b/templates/smarty-sample/connexion.html @@ -0,0 +1,27 @@ +{include file="includes/header.html"} + +{form name="thelia.customer.creation"} +
+ + {form_field_hidden form=$form} + {form_field form=$form.firstname} + {form_error form=$form.firstname} + {$message} + {/form_error} + + + {/form_field} + + {form_field form=$form.lastname} + {form_error form=$form.lastname} + {$message} + {/form_error} + + + {/form_field} + + +
+{/form} + +{include file="includes/footer.html"} \ No newline at end of file diff --git a/templates/smarty-sample/index.html b/templates/smarty-sample/index.html index 28f78f06c..e04fbf097 100755 --- a/templates/smarty-sample/index.html +++ b/templates/smarty-sample/index.html @@ -7,41 +7,8 @@ An image from asset directory :
{intl l='An internationalized string'}
-{form name="thelia.customer.creation"} -
- {form_field_hidden form=$form} - {form_field form=$form.email} - {form_error form=$form.email} - {$message} - {/form_error} - {intl l="{$label}"} : - {/form_field} - {form_field form=$form.name} - {intl l='name'} : - {/form_field} - {form_field form=$form.age} - {intl l='age'} : - {/form_field} - -
-{/form} - -
- -{form name="thelia.admin_login"} -
- {form_field_hidden form=$form} - {form_field form=$form.username} - username : - {/form_field} - - {form_field form=$form.password} - password : - {/form_field} -
-{/form}
jQuery data:
From b820afa9456efb9f408d5c7ce627edbc47ab8979 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Mon, 1 Jul 2013 16:27:43 +0200 Subject: [PATCH 10/10] continue to implement form customer --- core/lib/Thelia/Form/CustomerCreation.php | 6 ++- templates/smarty-sample/connexion.html | 50 ++++++++++++++++++++++- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/core/lib/Thelia/Form/CustomerCreation.php b/core/lib/Thelia/Form/CustomerCreation.php index 80efb74da..a8490fc43 100644 --- a/core/lib/Thelia/Form/CustomerCreation.php +++ b/core/lib/Thelia/Form/CustomerCreation.php @@ -68,12 +68,14 @@ class CustomerCreation extends BaseForm ->add("zipcode", "text", array( "constraints" => array( new Constraints\NotBlank() - ) + ), + "label" => "zipcode" )) ->add("country", "text", array( "constraints" => array( new Constraints\NotBlank() - ) + ), + "label" => "country" )) ->add("password", "password", array( "constraints" => array( diff --git a/templates/smarty-sample/connexion.html b/templates/smarty-sample/connexion.html index 69c4b516c..d16aa52e9 100644 --- a/templates/smarty-sample/connexion.html +++ b/templates/smarty-sample/connexion.html @@ -9,7 +9,7 @@ {$message} {/form_error} - +
{/form_field} {form_field form=$form.lastname} @@ -17,7 +17,53 @@ {$message} {/form_error} - +
+ {/form_field} + + {form_field form=$form.address1} + {form_error form=$form.address1} + {$message} + {/form_error} + +
+ {/form_field} + + {form_field form=$form.address2} + {form_error form=$form.address2} + {$message} + {/form_error} + +
+ {/form_field} + + {form_field form=$form.address3} + {form_error form=$form.address3} + {$message} + {/form_error} + +
+ {/form_field} + + {form_field form=$form.zipcode} + {form_error form=$form.zipcode} + {$message} + {/form_error} + +
+ {/form_field} + + {form_field form=$form.country} + {form_error form=$form.country} + {$message} + {/form_error} + + + +
{/form_field}