Reputation: 289
I have a plugin with a form where User can register, to avoid duplicate registrations I've added a custom Validator which checks if a FE user already contain the email.
//This is just the isValid method of the custom validator..
public function isValid($value): void
{
$existingUser = $this->frontendUserRepository->findByEmail($value)->getFirst();
if ($existingUser != null) {
$this->addError(
'E-mail already registered',
1592556619
);
}
}
Because of an additional double opt-in mechanism I would like to disable the new created FE user (which is created in the submit action of the registration form), so far so good. But now my custom validator didn't find users which are disabled when try to find it by email.
So my question is, how can I tell it that he have to ignore the disabled state of the entry.
Upvotes: 0
Views: 104
Reputation: 6460
You will need to configure your repository method to ignore the disabled
enable field. (See enablecolumns
in TCA.)
You will need to add a custom findOneByEmail()
method to your repository and configure the query accordingly:
$query = $this->createQuery();
$query->getQuerySettings()
->setIgnoreEnableFields(true)
->setEnableFieldsToBeIgnored(['disabled']);
Afterwards you can execute the query as usual which will now include disabled users:
$query
->matching($query->equals('email', $email))
->setLimit(1);
return $query->execute()->getFirst();
Notice that you should put this in a separate findOneByEmailIncludingHidden()
or similar to avoid unexpected side-effects and make the special behavior of this query method clear.
(You may have noticed that findOneBy*
is used here instead of findBy*
since that will automatically return the first object.)
Upvotes: 1