-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Form using Validator Constraint File - Max File Size not validating correctly, element returns NULL #13291
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
Comments
I tried to recreate this issue in a fork of the Symfony Standard Edition (v2.6) I came to the following conclusions, which are summarized in the DefaultController (https://github.com/lscholten/symfony-standard-issue-13291/blob/2.6/src/AppBundle/Controller/DefaultController.php) You passed all request data to the submit function:
This won't work correctly since all request data (not just the form data) is submitted. I fixed it and recreated your issue by using:
And indeed, the What happens is that
However, this could've all been avoided when using the recommended method
Conclusion: probably not a Symfony bug |
The weird thing is that, if the file does not exceed max size you don't need to do an array_merge, it works perfectly, and if the mime type validation fails, its also reported on the form errors, but its not the case for maxSize |
@lscholten excellent analysis! @humanoyd I just tried on @lscholten's fork and it indeed works as expected, consistently, no matter what the file size is. Could you please try it on that fork and alter the code so it shows how to reproduce your problem? |
Checkedout out the code, and tried /**
* @Route("/app/expected-behaviour", name="expected_behaviour")
*/
public function expectedBehaviourAction()
{
$form = $this->createFileForm();
if ($this->getRequest()->getMethod() === "POST") {
$form->submit(
array_merge(
$this->getRequest()->request->get($form->getName()),
$this->getRequest()->files->get($form->getName())
)
);
var_dump($form->get('my_file')->getData());
}
return $this->render('default/index.html.twig', ['form' => $form->createView()]);
} Getting:
|
@humanoyd what's |
UPDATE: if the file exceeds upload_max_filesize, exception is thrown:
Only $form->handleRequest($request); works properly with files greater than the upload_max_filesize |
Here is the analysis of 3 cases private function createFileForm()
{
return $this->createFormBuilder()
->add('my_file', 'file', [
'required' => true,
'constraints' => [
new File([
'maxSize' => '2M',
'mimeTypes' => [
'application/pdf',
'application/x-pdf',
],
'mimeTypesMessage' => 'Please upload a valid PDF',
])
]
])
->add('field2','text',['required'=>true])
->add('submit', 'submit')
->getForm();
} /**
* @Route("/app/original-not-working2", name="original_not_working")
*/
public function orginalNotWorkingAction()
{
$form = $this->createFileForm();
if ($this->getRequest()->getMethod() === "POST") {
$form->submit($this->getRequest());
var_dump($form->get('my_file')->getData());
}
return $this->render('default/index.html.twig', ['form' => $form->createView()]);
} Using 1. Case 1File: some-file.odt Expected: 2. Case 2:File size greater than validation rule, but less than php's upload_max_filesize 3. Case 3:File size greater than post_max_size |
@jakzal pushed the code to: |
@humanoyd excellent! I'll have a look later today or tomorrow. |
Looks like it was introduced when moving In both cases (
|
…eRequest Fixes the issue with uploaded files exceeding post_max_size, and form marked valid with empty fields
…eRequest Fixes the issue with uploaded files exceeding post_max_size, and form marked valid with empty fields
…eRequest When the argument of Form::submit is a Request object it forwards to Form::HandleRequest. Fixes the issue with uploaded files exceeding post_max_size, and form marked valid with empty fields.
To sum up: You have to use |
I've encountered the same problem when the file size exceeds upload_max_filesize and when using handleRequest. Config details below. In HttpFoundationRequestHandler.php::handleRequest, In ServerParams.php::getContentLength, I think getContentLength needs to check both CONTENT_LENGTH and HTTP_CONTENT_LENGTH. https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Util/ServerParams.php shows the same issue Submitted as #19311 Config: |
how handleRequest() works for you? For me it goes into block where is such comment " // Don't submit the form if it is not present in the request" . I checked form name is 'form'. here is what I have tried -
|
When submitting a file greater than the max file size, and then trying to get the UploadedFile, it returns null instead of an UploadedFile instance.
Actual:
Expected:
The text was updated successfully, but these errors were encountered: