/**
* {@inheritdoc}
*/
public function validate($emailTemplate, Constraint $constraint)
{
/** @var EmailTemplate $emailTemplate */
/** @var VariablesConstraint $constraint */
$fieldsToValidate = array('subject' => $emailTemplate->getSubject(), 'content' => $emailTemplate->getContent());
foreach ($emailTemplate->getTranslations() as $trans) {
if (in_array($trans->getField(), array('subject', 'content'))) {
$fieldsToValidate[$trans->getLocale() . '.' . $trans->getField()] = $trans->getContent();
}
}
$relatedEntity = false;
if (class_exists($emailTemplate->getEntityName())) {
$className = $emailTemplate->getEntityName();
$relatedEntity = new $className();
}
$errors = array();
foreach ($fieldsToValidate as $field => $value) {
try {
$this->twig->render($value, array('entity' => $relatedEntity, 'user' => $this->getUser()));
} catch (\Exception $e) {
$errors[$field] = true;
}
}
if (!empty($errors)) {
$this->context->addViolation($constraint->message);
}
}