verify if an email already exists in newsletter table

This commit is contained in:
Manuel Raynaud
2013-10-22 10:58:16 +02:00
parent cc4f4f0bbe
commit 47212490d2

View File

@@ -23,9 +23,12 @@
namespace Thelia\Form;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\ExecutionContextInterface;
use Thelia\Core\Translation\Translator;
use Thelia\Model\NewsletterQuery;
/**
@@ -62,7 +65,13 @@ class NewsletterForm extends BaseForm
->add('email', 'email', array(
'constraints' => array(
new NotBlank(),
new Email()
new Email(),
new Callback(array(
"methods" => array(
array($this,
"verifyExistingEmail")
)
))
),
'label' => Translator::getInstance()->trans('email'),
'label_attr' => array(
@@ -71,6 +80,14 @@ class NewsletterForm extends BaseForm
));
}
public function verifyExistingEmail($value, ExecutionContextInterface $context)
{
$customer = NewsletterQuery::create()->findOneByEmail($value);
if ($customer) {
$context->addViolation("This email already exists");
}
}
/**
* @return string the name of you form. This name must be unique
*/