From e15c53625ea04717ad0d07b9a9263d249199f20c Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Tue, 2 Jul 2013 11:48:34 +0200 Subject: [PATCH] complete CustomerCreation form --- core/lib/Thelia/Action/Customer.php | 1 + core/lib/Thelia/Form/CustomerCreation.php | 71 ++++++++++++++++++++--- core/lib/Thelia/Model/Customer.php | 1 + core/lib/Thelia/Model/CustomerQuery.php | 1 + templates/smarty-sample/connexion.html | 30 +++++++++- 5 files changed, 95 insertions(+), 9 deletions(-) diff --git a/core/lib/Thelia/Action/Customer.php b/core/lib/Thelia/Action/Customer.php index 7a26d40b6..bc1a3567c 100755 --- a/core/lib/Thelia/Action/Customer.php +++ b/core/lib/Thelia/Action/Customer.php @@ -50,6 +50,7 @@ class Customer implements EventSubscriberInterface if ($form->isValid()) { echo "ok"; exit; } else { + $event->setFormError($form); } } diff --git a/core/lib/Thelia/Form/CustomerCreation.php b/core/lib/Thelia/Form/CustomerCreation.php index a8490fc43..f03c99cc8 100644 --- a/core/lib/Thelia/Form/CustomerCreation.php +++ b/core/lib/Thelia/Form/CustomerCreation.php @@ -25,7 +25,10 @@ namespace Thelia\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\ExecutionContext; +use Symfony\Component\Validator\ExecutionContextInterface; use Thelia\Model\ConfigQuery; +use Thelia\Model\CustomerQuery; class CustomerCreation extends BaseForm @@ -46,13 +49,6 @@ class CustomerCreation extends BaseForm ), "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() @@ -77,15 +73,74 @@ class CustomerCreation extends BaseForm ), "label" => "country" )) + ->add("email", "email", array( + "constraints" => array( + new Constraints\NotBlank(), + new Constraints\Email(), + new Constraints\Callback(array( + "methods" => array( + $this, + "verifyExistingEmail" + ) + )) + ), + "label" => "email" + )) + ->add("email_confirm", "email", array( + "constraints" => array( + new Constraints\Callback(array( + "methods" => array( + $this, + "verifyEmailField" + ) + )) + ), + "label" => "email confirmation" + )) ->add("password", "password", array( "constraints" => array( new Constraints\Length(array("min" => ConfigQuery::read("password.length", 4))) - ) + ), + "label" => "password" + )) + ->add("password_confirm", "password", array( + "constraints" => array( + new Constraints\Length(array("min" => ConfigQuery::read("password.length", 4))), + new Constraints\Callback(array("methods" => array( + array($this, "verifyPasswordField") + ))) + ), + "label" => "password confirmation" )) ; } + public function verifyPasswordField($value, ExecutionContextInterface $context) + { + $data = $context->getRoot()->getData(); + + if ($data["password"] != $data["password_confirm"]) { + $context->addViolation("password confirmation is not the same as password field"); + } + } + + public function verifyEmailField($value, ExecutionContextInterface $context) + { + $data = $context->getRoot()->getData(); + + if ($data["email"] != $data["email_confirm"]) { + $context->addViolation("email confirmation is not the same as email field"); + } + } + + public function verifyExistingEmail($value, ExecutionContextInterface $context) + { + if (CustomerQuery::create()->filterByEmail($value)->exists()) { + $context->addViolation("This email already exists"); + } + } + public function getName() { return "customerCreation"; diff --git a/core/lib/Thelia/Model/Customer.php b/core/lib/Thelia/Model/Customer.php index 835f91e59..9e9018620 100755 --- a/core/lib/Thelia/Model/Customer.php +++ b/core/lib/Thelia/Model/Customer.php @@ -18,4 +18,5 @@ use Thelia\Model\om\BaseCustomer; */ class Customer extends BaseCustomer { + } diff --git a/core/lib/Thelia/Model/CustomerQuery.php b/core/lib/Thelia/Model/CustomerQuery.php index 0a9c9bcf2..ac97850bf 100755 --- a/core/lib/Thelia/Model/CustomerQuery.php +++ b/core/lib/Thelia/Model/CustomerQuery.php @@ -18,4 +18,5 @@ use Thelia\Model\om\BaseCustomerQuery; */ class CustomerQuery extends BaseCustomerQuery { + } diff --git a/templates/smarty-sample/connexion.html b/templates/smarty-sample/connexion.html index d16aa52e9..b3137152a 100644 --- a/templates/smarty-sample/connexion.html +++ b/templates/smarty-sample/connexion.html @@ -1,7 +1,7 @@ {include file="includes/header.html"} {form name="thelia.customer.creation"} -
+ {form_field_hidden form=$form} {form_field form=$form.firstname} @@ -66,6 +66,34 @@
{/form_field} + {form_field form=$form.email} + {form_error form=$form.email} + {#message} + {/form_error} +
+ {/form_field} + + {form_field form=$form.email_confirm} + {form_error form=$form.email_confirm} + {#message} + {/form_error} +
+ {/form_field} + + {form_field form=$form.password} + {form_error form=$form.password} + {#message} + {/form_error} +
+ {/form_field} + + {form_field form=$form.password_confirm} + {form_error form=$form.password_confirm} + {#message} + {/form_error} +
+ {/form_field} +
{/form}