symfony – how can i fetch whole entity using validation group?


Ask A Question





Login To post a question

I’m trying to implement reminding password using tokens.

I created form:

class RemindPasswordType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('email', EmailType::class, [
            'label' => 'label.email'
        ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => User::class,
            'validation_groups' => [
                'remind_password'
            ]
        ]);
    }
}

User’s entity:

class User implements UserInterface
{
    /**
     * @ORMId
     * @ORMGeneratedValue
     * @ORMColumn(type="integer")
     */
    private $id;

    /**
     * @ORMColumn(type="string")
     * @AssertNotBlank(message="default.not_blank")
     * @AssertEmail(message="default.email")
     * @AcmeAssertExistentEmail(groups= {"remind_password"})
     */
    private $email;

    /**
     * @ORMColumn(type="string", nullable=true)
     */
    private $remindToken;
}

ExistentEmailValidator:

    class ExistentEmailValidator extends ConstraintValidator
    {
        private $accountRepository;

        public function __construct(AccountRepository $accountRepository)
        {
            $this->accountRepository = $accountRepository;
        }

        public function validate($value, Constraint $constraint)
        {
            if (!$this->accountRepository->findAccountByEmail($value)) {
                $this->context->buildViolation($constraint->message)->addViolation();
         }
    }

Problem:

$form->getData() returns a User object that contains only email field. Im forced to call findAccountByEmail method again in Controller. i would like to prevent that. Actually, im wondering if i should use validation_group in User’s entity class. What is proper way to implement reminding password?

For more info please click herehttps://stackoverflow.com/q/51279630



Have a question or need a custom quote?
info@smartinfocare.com