Skip to content

Added example for direct form submit with validation #11905

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

Merged
merged 1 commit into from
Sep 24, 2019
Merged
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
5 changes: 5 additions & 0 deletions form/direct_submit.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,8 @@ method, pass the submitted data directly to
"PATCH" method, the validation extension will only handle the submitted
fields. If the underlying data needs to be validated, this should be done
manually, i.e. using the validator.

When you need validation for some fields you can extend your data array with the required ones so that they will be validated.
Copy link
Member

Choose a reason for hiding this comment

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

I actually do not understand this addition. This block talks about partial submission of data. Adding more data here seems to defeat the use of a PATCH request as long as I do not miss anything.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem was that I had a contraints option in my UpdateUserProfileType.
This was not thrown an exception without that because of this clearMissing which not throws for not filled fields - as you can read here: symfony/symfony#25331
So then I found out that moving this constraints option to the entity direct made it work's in the form.

So now instead of having...

->add('email', EmailType::class, ['constraints' => [
    new Assert\Email(['groups' => ['profile_update', 'user_registration']]),
]])

I have only this...

->add('email', EmailType::class)

And then my entity got's this.

/**
 * @var string
 *
 * @Assert\Email(groups={"profile_update", "user_registration"})
 */
protected $email;

And then it's working.

But I can not use the constraints options because it will not validate when the user not PATCHing it. Thats the problem.
So my "moving contraints option to entity" solves the problem but not the root cause.

Copy link
Member

Choose a reason for hiding this comment

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

Then I think we need to expand explanations on the constraints option instead to make it clear that constraints are ignored if the field they are bound to is not submitted.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's what I want to try with this commit with the example 😉
But now you understand what I want/mean.

Copy link
Member

Choose a reason for hiding this comment

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

see #12365

i.e.::

$form->submit(array_merge(['email' => null, 'username' => null], $request->request->all()), false);