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

Symfony 3.2 isValid always return false #20805

Closed
dominikzogg opened this issue Dec 7, 2016 · 11 comments
Closed

Symfony 3.2 isValid always return false #20805

dominikzogg opened this issue Dec 7, 2016 · 11 comments

Comments

@dominikzogg
Copy link

dominikzogg commented Dec 7, 2016

the following code worked within symfony 2.8 (patch action), after updating to symfony 3.2 i always get into the if statement, even if there are no errors. calling the validator myself, would tell everything is fine.

$form = $this->createForm(UserType::USER_UPDATE, $user);
$form->submit($request->request->all(), false);
if (false === $form->isValid()) {
    ....
}

Do i missed a change, that does not throw an exception, but ends in this strange behavior?

I already refactored the createForm to:

$form = $this->createForm(UserUpdateType::class, $user);
@HeahDude
Copy link
Contributor

HeahDude commented Dec 7, 2016

Hi @dominikzogg, you should read the 3.2 upgrade file and call:

if ($form->isSubmitted() && false === $form->isValid()) {
    ....
}

Does it solve your issue?

@xabbuh xabbuh added the Form label Dec 7, 2016
@dominikzogg
Copy link
Author

@HeahDude no it does not, i saw within sample, but as you can see within submit() $this->submitted = true; will always be set

@dominikzogg
Copy link
Author

dominikzogg commented Dec 7, 2016

$form->submit($request->request->all(), false);

$violationList = $this->get('validator')->validate($user);
if (0 !== $violationList->count()) {...}

doing it this way works perfect, which tells me validation works

@HeahDude
Copy link
Contributor

HeahDude commented Dec 7, 2016

Hmm, looks weird. Could you please fork symfony standard and add at the minimum of code to reproduce this? Thanks!

@Hanmac
Copy link
Contributor

Hanmac commented Dec 7, 2016

you shouldn't use submit, which seems to be the problem.

this is how i do it on my systems:

    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {

     }

use this to get the Request:

public function myAction(Request $request) {
}

@dominikzogg
Copy link
Author

@Hanmac handleRequest is bad for api, cause the missing second argument which allows to ignore not send values

@dominikzogg
Copy link
Author

@HeahDude i can live with duplicate validation for the moment, can't spend the time to strip down this project (big, closed source onces).

@HeahDude
Copy link
Contributor

@dominikzogg can you please try the patch from #20966, to see if this about a transformation failure?

Another possibility is that using false as second argument cause some field(s) to not be submitted while the user pre set data passed at form's creation contains invalid data. See #11493 and #18935.

@dominikzogg
Copy link
Author

@HeahDude #20996 is a patch for sf2.7, adapting it does not change anything in my usecase. the problem only happen on POST, PATCH (with second parameter) works.

@xabbuh
Copy link
Member

xabbuh commented Dec 31, 2016

@dominikzogg I am closing here for now as we cannot solve the issue without a reproducable example. However, I will happily reopen once we have such a fork of the Symfony Standard Edition.

@xabbuh xabbuh closed this as completed Dec 31, 2016
@xabbuh
Copy link
Member

xabbuh commented Feb 15, 2019

Can you confirm that the changes proposed in #30265 will fix this issue?

xabbuh added a commit that referenced this issue Feb 21, 2019
…requests (xabbuh)

This PR was merged into the 3.4 branch.

Discussion
----------

[Form] do not validate non-submitted form fields in PATCH requests

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #11493, #19788, #20805, #24453, #30011
| License       | MIT
| Doc PR        |

When a form field is not embedded as part of a HTTP PATCH requests, its
validation constraints configured through the `constraints` option must
not be evaluated. The fix from #10567 achieved this by not mapping their
violations to the underlying form field. This however also means that
constraint violations caused by validating the whole underlying data
object will never cause the form to be invalid. This breaks use cases
where some constraints may, for example, depend on the value of other
properties that were changed by the submitted data.

Commits
-------

a60d802 do not validate non-submitted form fields in PATCH requests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants