I have a Symfony form that has fields that should let users write emails but not URLs.
I am currently using this regex:
return new Regex(
[
'pattern' => '((http|https|ftp|ftps)://)?([a-zA-Z0-9-]*.)+[a-zA-Z0-9]{2,4}(/[a-zA-Z0-9=.?&-]*)?',
'match' => false,
'message' => $this->translator->trans('form.field.urlNotAllowed', ['%label%' => $label])
]
);
This regex matches all URLs, but also match emails for validation.
What I want to do is excluding emails from validation and match only URLs.
My code:
/**
* @param RegistrationFormField $field
* @param string $key
* @param array $validationAttributes
* @return Regex
*/
public function getUrlNotAllowedConstraint($field, $key, &$validationAttributes)
{
$event = $field->getRegistrationForm()->getEvent();
$label = /** @Ignore */
$this->translator->trans($field->getLabel(), [], 'custom') ?: $this->getDefaultLabelName($event, $key);
$validationAttributes['data-validation'][] = 'url_not_allowed';
return new Regex(
[
'pattern' => '((http|https|ftp|ftps)://)?([a-zA-Z0-9-]*.)+[a-zA-Z0-9]{2,4}(/[a-zA-Z0-9=.?&-]*)?',
'match' => false,
'message' => $this->translator->trans('form.field.urlNotAllowed', ['%label%' => $label])
]
);
}
Any help?