[
'constraints' => [
NotBlank::class => ['message' => 'mautic.form.submission.captcha.invalid'],
EqualTo::class => ['message' => 'mautic.form.submission.captcha.invalid'],
Blank::class => ['message' => 'mautic.form.submission.captcha.invalid'],
],
],
'checkboxgrp' => [],
'country' => [],
'date' => [],
'datetime' => [],
'email' => [
'filter' => 'email',
'constraints' => [
Email::class => ['message' => 'mautic.form.submission.email.invalid'],
],
],
'freetext' => [],
'freehtml' => [],
'hidden' => [],
'number' => [
'filter' => 'float',
],
'pagebreak' => [],
'password' => [],
'radiogrp' => [],
'select' => [],
'tel' => [],
'text' => [],
'textarea' => [],
'url' => [
'filter' => 'url',
'constraints' => [
Url::class => ['message' => 'mautic.form.submission.url.invalid'],
],
],
'file' => [],
];
public function __construct(Translator $translator, ValidatorInterface $validator = null)
{
$this->translator = $translator;
if (null === $validator) {
$validator = $validator = Validation::createValidator();
}
$this->validator = $validator;
parent::__construct();
}
/**
* Set the translation key prefix.
*/
public function setTranslationKeyPrefix(): void
{
$this->translationKeyPrefix = 'mautic.form.field.type.';
}
/**
* @param array $customFields
*
* @deprecated to be removed in 3.0; use getChoiceList($customFields = []) instead
*
* @return array
*/
public function getList($customFields = [])
{
return $this->getChoiceList($customFields);
}
/**
* @return array
*/
public function getTypes()
{
return $this->types;
}
/**
* Get fields input filter.
*
* @return string
*/
public function getFieldFilter($type)
{
if (array_key_exists($type, $this->types)) {
return $this->types[$type]['filter'] ?? 'clean';
}
return 'alphanum';
}
/**
* @param Field $f
*/
public function validateFieldValue($type, $value, $f = null): array
{
$errors = [];
if (isset($this->types[$type]['constraints'])) {
foreach ($this->types[$type]['constraints'] as $constraint => $opts) {
// don't check empty values unless the constraint is NotBlank
if (NotBlank::class === $constraint && empty($value)) {
continue;
}
if ('captcha' == $type) {
$captcha = $f->getProperties()['captcha'];
if (empty($captcha) && Blank::class !== $constraint) {
// Used as a honeypot
$captcha = '';
} elseif (Blank::class === $constraint) {
continue;
}
if (EqualTo::class == $constraint) {
$opts['value'] = $captcha;
}
}
/** @var ConstraintViolationList $violations */
$violations = $this->validator->validate($value, new $constraint($opts));
if (count($violations)) {
/** @var ConstraintViolation $v */
foreach ($violations as $v) {
$transParameters = $v->getParameters();
if (null !== $f) {
$transParameters['%label%'] = '"'.$f->getLabel().'"';
}
$errors[] = $this->translator->trans($v->getMessage(), $transParameters, 'validators');
}
}
}
}
return $errors;
}
/**
* Search and replace the HTML of the form field with the value.
*/
public function populateField($field, $value, $formName, &$formHtml): void
{
$alias = $field->getAlias();
switch ($field->getType()) {
case 'text':
case 'number':
case 'email':
case 'hidden':
case 'tel':
case 'url':
case 'date':
case 'datetime':
if ('tel' === $field->getType()) {
$sanitizedValue = InputHelper::clean($value);
} else {
$sanitizedValue = $this->sanitizeValue($value);
}
if (preg_match('//i', $formHtml, $match)) {
$replace = '';
$formHtml = str_replace($match[0], $replace, $formHtml);
}
break;
case 'textarea':
if (preg_match('/