Skip to content

Instantly share code, notes, and snippets.

@georgringer
Last active December 14, 2020 19:16
Show Gist options
  • Save georgringer/eb9d84ecdcb489ea8d6fda36eaef3a07 to your computer and use it in GitHub Desktop.
Save georgringer/eb9d84ecdcb489ea8d6fda36eaef3a07 to your computer and use it in GitHub Desktop.
Powermail check if value is same in other fields, like for email double validation
<?php
namespace VENDOR\EXT\Domain\Powermail;
use In2code\Powermail\Domain\Model\Mail;
use In2code\Powermail\Domain\Validator\AbstractValidator;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Error\Result;
class EmailValidator extends AbstractValidator
{
protected $configuration = [];
/**
*
* @param Mail $mail
* @return bool
*/
public function isValid($mail)
{
$result = new Result();
if ((int)$this->configuration['form'] === $mail->getForm()->getUid()) {
$answerValues = [];
$firstAnswerField = null;
$fields = GeneralUtility::trimExplode(',', $this->configuration['emailFields'], true);
foreach ($mail->getAnswers() as $answer) {
if ($firstAnswerField === null) {
$firstAnswerField = $answer->getField();
}
if (in_array($answer->getField()->getMarker(), $fields, true)) {
$answerValues[] = $answer->getValue();
}
}
if (count($answerValues) > 1) {
$isSame = true;
foreach ($answerValues as $value) {
if ($value !== $answerValues[0]) {
$isSame = false;
}
}
if (!$isSame) {
$this->setErrorAndMessage($firstAnswerField, 'Email-Adresse ist nicht ident');
}
}
}
return $this->isValidState();
}
}
plugin.tx_powermail.settings.setup {
validators {
1 {
# Classname that should be called with method *Validator()
class = VENDOR\EXT\Domain\Powermail\EmailValidator
# optional: Add configuration for your PHP
config {
emailFields = e_mail,email2
form = 1
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment