From 995e1eb679954c64d106e82a1ee27a33f3cb8f3e Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Fri, 28 Jun 2013 11:36:35 +0200 Subject: [PATCH 1/3] 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 2/3] 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 3/3] 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(