I have integrated in my Symfony project the FOSUserBundle with the HWIOAUTHBundle. I have a facebook and a google login/registration next to the simple login form from FOSUserBundle. However, Facebook doesn't necessarily give me an email back, since it's not needed for a Facebook account (i.e when the user registers on Facebook via phone). In this case, when the user registers I set her/his email address to be the same as the facebook/google ID (because I can't leave email field empty).
When somebody on my website orders an item, I need to send him an email containing a QR code that she/he will use to authenticate himself, thus I need a way to get a real e-mail address from the user.
I have thought that when the user registers via Facebook and tries to purchase a product I'd redirect her/him to the profile edit page, show a notification that he/she should provide a correct email address and then she/he can move on with the purchase.
However, I need to validate if their current email is a real (or at least real-looking) email address in the controller that handles the purchases.
How can I use Symfony's validator in a controller to check if the user's email from $user = $this->get('security.token_storage')->getToken()->getUser(); $user->getEmail();
really looks like an email?
For now this is what I have:
if (!$this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
throw $this->createAccessDeniedException('some message');
}
$user = $this->get('security.token_storage')->getToken()->getUser();
if ($user->isEnabled() == false) {
throw $this->createAccessDeniedException('some message');
}
if (null == $user->getEmail() || $user->getFacebookId() == $user->getEmail() || $user->getGoogleId() === $user->getEmail()) {
$session = new Session();
$session->getFlashBag()->add('warning', 'Please provide a real email address, yata yata, etc.');
return $this->redirectToRoute('fos_user_profile_edit');
}
Thanks in advance!
Try this in your controller
...
use Symfony\Component\Validator\Validator\ValidatorInterface;
// ...
public function addEmailAction($email, ValidatorInterface $validator)
{
$emailConstraint = new Assert\Email();
// all constraint "options" can be set this way
$emailConstraint->message = 'Invalid email address';
// use the validator to validate the value
$errorList = $validator->validate(
$email,
$emailConstraint
);
See the doc here https://symfony.com/doc/current/validation/raw_values.html