complete CustomerCreation form

This commit is contained in:
Manuel Raynaud
2013-07-02 11:48:34 +02:00
parent 6f4c2f0376
commit e15c53625e
5 changed files with 95 additions and 9 deletions

View File

@@ -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";