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 (ie 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();
$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
You should be able to just use the validator
service and feed it with the value and the constraint (or a list of combined constraints)
This simple example should work (for sf 3.3+ depending on your service definition strategy you may have to inject it through the constructor)
public function testAction()
{
$constraint = new \Symfony\Component\Validator\Constraints\Email();
$stringToTest = 'lorem@ipsum.com';
$errors = $this->get('validator')->validate($stringToTest, $constraint);
if(count($errors) > 0) {
//no valid email
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.