i working on registration form need put validation on email id, if email domain not belong specific domain person should not able register question symfony default has validation option can turn on or need create custom validation?
for example want people register if email id has yahoo.com
no, there's not build-in feature in symfony2 check domain email. can add it. can creating custom constraint.
namespace appbundle\validator\constraints; use symfony\component\validator\constraint; /** * @annotation */ class emaildomain extends constraint { public $domains; public $message = 'the email "%email%" has not valid domain.'; }
namespace appbundle\validator\constraints; use symfony\component\validator\constraint; use symfony\component\validator\constraintvalidator; class emaildomainvalidator extends constraintvalidator { public function validate($value, constraint $constraint) { $explodedemail = explode('@', $value); $domain = array_pop($explodedemail); if (!in_array($domain, $constraint->domains)) { $this->context->buildviolation($constraint->message) ->setparameter('%email%', $value) ->addviolation(); } } }
after can use new validator:
use symfony\component\validator\constraints assert; use appbundle\validator\constraints customassert; class myentity { /** * @assert\email() * @customassert\emaildomain(domains = {"yahoo.com", "gmail.com"}) */ protected $email;
in case needs add validation inside .yml
file here how can it.
- appbundle\validator\constraints\emaildomain: domains: - yahoo.com
Comments
Post a Comment