Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Form] Fix Array to string conversion #20935

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/TextType.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ public function buildForm(FormBuilderInterface $builder, array $options)
if ('' === $options['empty_data']) {
$builder->addViewTransformer($this);
}

$builder->addEventListener(
FormEvents::PRE_SUBMIT,
function (FormEvent $event) {
if (is_array($event->getData())) {
$event->setData('');
$event->getForm()->addError(
new FormError('Invalid value.')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest you to add error this way:

new FormError('This value should be of type {{ type }}.', null, ['{{ type }}' => 'string'])

Thus, it uses an already existing error message which is convenient for translations.
What do you think about it ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This solution is completely wrong. Read the comments above for details.

);
}
}
);
}

/**
Expand Down