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

Conversation

patrickbussmann
Copy link
Contributor

See symfony/symfony#25331.

I had the same issue.
My registration form is filled via an API call and this call contains only filled fields.
But when the user is missing all fields its an empty array and then the username and email will not be validated.
After I found out that this is the behaviour I want to share this knowledge ;-)

@@ -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

@javiereguiluz javiereguiluz changed the base branch from master to 3.4 September 24, 2019 15:07
javiereguiluz added a commit that referenced this pull request Sep 24, 2019
…trickbussmann)

This PR was submitted for the master branch but it was merged into the 3.4 branch instead (closes #11905).

Discussion
----------

Added example for direct form submit with validation

See symfony/symfony#25331.

I had the same issue.
My registration form is filled via an API call and this call contains only filled fields.
But when the user is missing all fields its an empty array and then the username and email will not be validated.
After I found out that this is the behaviour I want to share this knowledge ;-)

Commits
-------

c81d30a Added example for direct form submit with validation
@javiereguiluz javiereguiluz merged commit c81d30a into symfony:3.4 Sep 24, 2019
@javiereguiluz
Copy link
Member

Patrick, thanks for this contribution. We finally merged it after a minor reword.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants