symfony 中有一个电子邮件验证器,可以在表单中使用: http: //symfony.com/doc/current/reference/constraints/Email.html
我的问题是:如何在我的控制器中使用此验证器来验证电子邮件地址?
这可以通过使用 PHP preg_match for usere 来实现,但我的问题是是否有可能使用已经内置于电子邮件验证器中的 Symfony。
先感谢您。
symfony 中有一个电子邮件验证器,可以在表单中使用: http: //symfony.com/doc/current/reference/constraints/Email.html
我的问题是:如何在我的控制器中使用此验证器来验证电子邮件地址?
这可以通过使用 PHP preg_match for usere 来实现,但我的问题是是否有可能使用已经内置于电子邮件验证器中的 Symfony。
先感谢您。
通过使用Validator服务的validateValue方法
use Symfony\Component\Validator\Constraints\Email as EmailConstraint;
// ...
public function customAction()
{
$email = 'value_to_validate';
// ...
$emailConstraint = new EmailConstraint();
$emailConstraint->message = 'Your customized error message';
$errors = $this->get('validator')->validateValue(
$email,
$emailConstraint
);
// $errors is then empty if your email address is valid
// it contains validation error message in case your email address is not valid
// ...
}
// ...
我写了一篇关于在表单之外验证电子邮件地址(一个或多个)的帖子
它还涵盖了一个常见错误,您可以在该错误中验证电子邮件约束并忘记 NotBlank
/**
* Validates a single email address (or an array of email addresses)
*
* @param array|string $emails
*
* @return array
*/
public function validateEmails($emails){
$errors = array();
$emails = is_array($emails) ? $emails : array($emails);
$validator = $this->container->get('validator');
$constraints = array(
new \Symfony\Component\Validator\Constraints\Email(),
new \Symfony\Component\Validator\Constraints\NotBlank()
);
foreach ($emails as $email) {
$error = $validator->validateValue($email, $constraints);
if (count($error) > 0) {
$errors[] = $error;
}
}
return $errors;
}
我希望这有帮助
如果您在控制器本身中创建表单并希望在操作中验证电子邮件,那么代码将如下所示。
// add this above your class
use Symfony\Component\Validator\Constraints\Email;
public function saveAction(Request $request)
{
$form = $this->createFormBuilder()
->add('email', 'email')
->add('siteUrl', 'url')
->getForm();
if ('POST' == $request->getMethod()) {
$form->bindRequest($request);
// the data is an *array* containing email and siteUrl
$data = $form->getData();
// do something with the data
$email = $data['email'];
$emailConstraint = new Email();
$emailConstraint->message = 'Invalid email address';
$errorList = $this->get('validator')->validateValue($email, $emailConstraint);
if (count($errorList) == 0) {
$data = array('success' => true);
} else {
$data = array('success' => false, 'error' => $errorList[0]->getMessage());
}
}
return $this->render('AcmeDemoBundle:Default:update.html.twig', array(
'form' => $form->createView()
));
}
我也是新手,正在学习它,任何建议将不胜感激......
为什么没有人提到您可以使用“约束”键在 FormBuilder 实例中验证它???首先,阅读文档Using a Form without a Class
'constraints' =>[
new Assert\Email([
'message'=>'This is not the corect email format'
]),
new Assert\NotBlank([
'message' => 'This field can not be blank'
])
],
适用于 symfony 3.1
例子:
namespace SomeBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\Extension\Core\Type;
use Symfony\Component\Validator\Constraints as Assert;
class DefaultController extends Controller
{
/**
* @Route("kontakt", name="_kontakt")
*/
public function userKontaktAction(Request $request) // access for all
{
$default = array('message' => 'Default input value');
$form = $this->createFormBuilder($default)
->add('name', Type\TextType::class,[
'label' => 'Nazwa firmy',
])
->add('email', Type\EmailType::class,[
'label' => 'Email',
'constraints' =>[
new Assert\Email([
'message'=>'This is not the corect email format'
]),
new Assert\NotBlank([
'message' => 'This field can not be blank'
])
],
])
->add('phone', Type\TextType::class,[
'label' => 'Telefon',
])
->add('message', Type\TextareaType::class,[
'label' => 'Wiadomość',
'attr' => [
'placeholder' => 'Napisz do nas ... '
],
])
->add('send', Type\SubmitType::class,[
'label' => 'Wyślij',
])
->getForm();
$form->handleRequest($request);
if ($form->isValid()) {
// data is an array with "name", "email", and "message" keys
$data = $form->getData();
// send email
// redirect to prevent resubmision
var_dump($data);
}
return $this->render('SomeBundle:Default:userKontakt.html.twig', [
'form' => $form->createView()
]);
}
}
请参阅有关可用验证类型的文档。 http://api.symfony.com/3.1/Symfony/Component/Validator/Constraints.html
如果您想检查除 message 之外的可用键,请转到以下位置的文档:
http://symfony.com/doc/current/reference/constraints/Email.html
或导航至:
YourProject\vendor\symfony\symfony\src\Symfony\Component\Validator\Constraints\Email.php
从那里,您将能够看到还有什么可用的。
public $message = 'This value is not a valid email address.'; public $checkMX = false; public $checkHost = false; public $strict; "
另请注意,我在控制器内创建并验证了表单,这不是最佳实践,只能用于表单,您永远不会在应用程序的其他任何地方重复使用。
最佳实践是在 YourBundle/Form 下的单独目录中创建表单。将所有代码移至新的 ContactType.php 类。(不要忘记在那里导入 FormBuilder 类,因为它不会扩展您的控制器,也无法通过 '$this' 访问此类)
[在 ContactType 类中:]
namespace AdminBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type;
use Symfony\Component\Validator\Constraints as Assert;
[在您的控制器内部:]
use YourBundle/Form/ContactType;
// use ...
//...
$presetData = []; //... preset form data here if you want to
$this->createForm('AdminBundle\Form\FormContactType', $presetData) // instead of 'createFormBuilder'
->getForm();
// render view and pass it to twig templet...
// or send the email/save data to database and redirect the form
另一种方式 - 您可以使用egulias/EmailValidator捆绑包。
composer require egulias/email-validator
它可以在没有容器的情况下使用
use Egulias\EmailValidator\EmailValidator;
use Egulias\EmailValidator\Validation\RFCValidation;
$validator = new EmailValidator();
$validator->isValid("example@example.com", new RFCValidation());
bundle也可以使用 DNSCheckValidation验证 DNS
我对 symfony 3 的解决方案如下:
use Symfony\Component\Validator\Constraints\Email as EmailConstraint;
$email = 'someinvalidmail@invalid.asdf';
// ... in the action then call
$emailConstraint = new EmailConstraint();
$errors = $this->get('validator')->validate(
$email,
$emailConstraint
);
$mailInvalid = count($errors) > 0;