diff --git a/bundle/InfoCollection/Form/Extension/EmailFormExtension.php b/bundle/InfoCollection/Form/Extension/EmailFormExtension.php new file mode 100644 index 00000000..4ccb48ed --- /dev/null +++ b/bundle/InfoCollection/Form/Extension/EmailFormExtension.php @@ -0,0 +1,51 @@ +setDefault('translation_domain', 'validators'); + } + + public function buildForm(FormBuilderInterface $builder, array $options): void + { + foreach ($builder->getData()->getCollectedFields() as $identifier => $fieldData) { + /** @var FieldData $fieldData */ + if ($fieldData->fieldDefinition->fieldTypeIdentifier === 'ezemail') { + $emailOptions = $builder->get($identifier)->getOptions(); + + $customEmailExists = false; + foreach ($emailOptions['constraints'] as $constraint) { + if ($constraint instanceof CustomEmail) { + $customEmailExists = true; + + break; + } + } + + if (!$customEmailExists) { + $emailOptions['constraints'][] = new CustomEmail(); + } + + $builder->add($identifier, InformationCollectionFieldType::class, $emailOptions); + } + } + } +} diff --git a/bundle/InfoCollection/Form/Extension/RequiredFieldFormExtension.php b/bundle/InfoCollection/Form/Extension/RequiredFieldFormExtension.php new file mode 100644 index 00000000..c5d2ca65 --- /dev/null +++ b/bundle/InfoCollection/Form/Extension/RequiredFieldFormExtension.php @@ -0,0 +1,56 @@ +setDefaults([ + 'translation_domain' => 'validators', + 'attr' => [ + 'class' => 'embed-form js-form-embed', + ], + ]); + } + + public function buildForm(FormBuilderInterface $builder, array $options): void + { + foreach ($builder->getData()->getCollectedFields() as $identifier => $fieldData) { + /** @var FieldData $fieldData */ + if ($fieldData->fieldDefinition->isRequired === true) { + $fieldOptions = $builder->get($identifier)->getOptions(); + + $hasRequiredFieldConstraint = false; + + foreach ($fieldOptions['constraints'] as $constraint) { + if ($constraint instanceof RequiredField) { + $hasRequiredFieldConstraint = true; + + break; + } + } + + if (!$hasRequiredFieldConstraint) { + $fieldOptions['constraints'][] = new RequiredField(); + $builder->add($identifier, InformationCollectionFieldType::class, $fieldOptions); + } + } + } + } +} diff --git a/bundle/InfoCollection/Validator/Constraints/CustomEmail.php b/bundle/InfoCollection/Validator/Constraints/CustomEmail.php new file mode 100644 index 00000000..de2f785c --- /dev/null +++ b/bundle/InfoCollection/Validator/Constraints/CustomEmail.php @@ -0,0 +1,12 @@ +value->email; + + if (filter_var($emailValue, FILTER_VALIDATE_EMAIL) === false) { + $this->context->buildViolation($constraint->message) + ->atPath($value->fieldDefinition->identifier) + ->addViolation(); + } + } +} diff --git a/bundle/InfoCollection/Validator/Constraints/RequiredField.php b/bundle/InfoCollection/Validator/Constraints/RequiredField.php new file mode 100644 index 00000000..2148ff02 --- /dev/null +++ b/bundle/InfoCollection/Validator/Constraints/RequiredField.php @@ -0,0 +1,12 @@ +value === null + || array_filter( + $value->value->attributes(), + static fn ($attr) => $value->value->attribute($attr) === null || $value->value->attribute($attr) === '' || $value->value->attribute($attr) === false, + ) !== [] + ) { + if (!property_exists($constraint, 'message')) { + return; + } + $this->context->buildViolation($constraint->message) + ->atPath($value->fieldDefinition->identifier) + ->addViolation(); + } + } +} diff --git a/bundle/Resources/config/info_collection.yaml b/bundle/Resources/config/info_collection.yaml index c80cf59a..02566be3 100644 --- a/bundle/Resources/config/info_collection.yaml +++ b/bundle/Resources/config/info_collection.yaml @@ -23,6 +23,16 @@ services: tags: - { name: netgen_information_collection.action } + ngsite.info_collection.form.extension.required_field: + class: Netgen\Bundle\SiteBundle\InfoCollection\Form\Extension\RequiredFieldFormExtension + tags: + - { name: form.type_extension, extended_type: Netgen\Bundle\InformationCollectionBundle\Ibexa\ContentForms\InformationCollectionType, priority: -100 } + + ngsite.info_collection.form.extension.email_form_extenison: + class: Netgen\Bundle\SiteBundle\InfoCollection\Form\Extension\EmailFormExtension + tags: + - { name: form.type_extension, extended_type: Netgen\Bundle\InformationCollectionBundle\Ibexa\ContentForms\InformationCollectionType, priority: -100 } + ngsite.info_collection.form.extension.honeypot_extension: class: Netgen\Bundle\SiteBundle\InfoCollection\Form\Extension\HoneypotExtension tags: diff --git a/bundle/Resources/translations/validators.en.yaml b/bundle/Resources/translations/validators.en.yaml new file mode 100644 index 00000000..fdf83cd2 --- /dev/null +++ b/bundle/Resources/translations/validators.en.yaml @@ -0,0 +1,2 @@ +this_value_should_not_be_blank: 'This value should not be blank.' +email_is_not_valid: 'Email is not valid.' \ No newline at end of file