예제 #1
0
 /**
  * {@inheritdoc}
  */
 public function handleRequest(FormInterface $form, $request = null)
 {
     if (null !== $request) {
         throw new UnexpectedTypeException($request, 'null');
     }
     $name = $form->getName();
     $method = $form->getConfig()->getMethod();
     if ($method !== self::getRequestMethod()) {
         return;
     }
     // For request methods that must not have a request body we fetch data
     // from the query string. Otherwise we look for data in the request body.
     if ('GET' === $method || 'HEAD' === $method || 'TRACE' === $method) {
         if ('' === $name) {
             $data = $_GET;
         } else {
             // Don't submit GET requests if the form's name does not exist
             // in the request
             if (!isset($_GET[$name])) {
                 return;
             }
             $data = $_GET[$name];
         }
     } else {
         // Mark the form with an error if the uploaded size was too large
         // This is done here and not in FormValidator because $_POST is
         // empty when that error occurs. Hence the form is never submitted.
         if ($this->serverParams->hasPostMaxSizeBeenExceeded()) {
             // Submit the form, but don't clear the default values
             $form->submit(null, false);
             $form->addError(new FormError($form->getConfig()->getOption('post_max_size_message'), null, array('{{ max }}' => $this->serverParams->getNormalizedIniPostMaxSize())));
             return;
         }
         $fixedFiles = array();
         foreach ($_FILES as $fileKey => $file) {
             $fixedFiles[$fileKey] = self::stripEmptyFiles(self::fixPhpFilesArray($file));
         }
         if ('' === $name) {
             $params = $_POST;
             $files = $fixedFiles;
         } elseif (array_key_exists($name, $_POST) || array_key_exists($name, $fixedFiles)) {
             $default = $form->getConfig()->getCompound() ? array() : null;
             $params = array_key_exists($name, $_POST) ? $_POST[$name] : $default;
             $files = array_key_exists($name, $fixedFiles) ? $fixedFiles[$name] : $default;
         } else {
             // Don't submit the form if it is not present in the request
             return;
         }
         if (is_array($params) && is_array($files)) {
             $data = array_replace_recursive($params, $files);
         } else {
             $data = $params ?: $files;
         }
     }
     // Don't auto-submit the form unless at least one field is present.
     if ('' === $name && count(array_intersect_key($data, $form->all())) <= 0) {
         return;
     }
     $form->submit($data, 'PATCH' !== $method);
 }
 /**
  * {@inheritdoc}
  */
 public function handleRequest(FormInterface $form, $request = null)
 {
     if (!$request instanceof Request) {
         throw new UnexpectedTypeException($request, 'Symfony\\Component\\HttpFoundation\\Request');
     }
     $name = $form->getName();
     $method = $form->getConfig()->getMethod();
     if ($method !== $request->getMethod()) {
         return;
     }
     // For request methods that must not have a request body we fetch data
     // from the query string. Otherwise we look for data in the request body.
     if ('GET' === $method || 'HEAD' === $method || 'TRACE' === $method) {
         if ('' === $name) {
             $data = $request->query->all();
         } else {
             // Don't submit GET requests if the form's name does not exist
             // in the request
             if (!$request->query->has($name)) {
                 return;
             }
             $data = $request->query->get($name);
         }
     } else {
         // Mark the form with an error if the uploaded size was too large
         // This is done here and not in FormValidator because $_POST is
         // empty when that error occurs. Hence the form is never submitted.
         $contentLength = $this->serverParams->getContentLength();
         $maxContentLength = $this->serverParams->getPostMaxSize();
         if (!empty($maxContentLength) && $contentLength > $maxContentLength) {
             // Submit the form, but don't clear the default values
             $form->submit(null, false);
             $form->addError(new FormError($form->getConfig()->getOption('post_max_size_message'), null, array('{{ max }}' => $this->serverParams->getNormalizedIniPostMaxSize())));
             return;
         }
         if ('' === $name) {
             $params = $request->request->all();
             $files = $request->files->all();
         } elseif ($request->request->has($name) || $request->files->has($name)) {
             $default = $form->getConfig()->getCompound() ? array() : null;
             $params = $request->request->get($name, $default);
             $files = $request->files->get($name, $default);
         } else {
             // Don't submit the form if it is not present in the request
             return;
         }
         if (is_array($params) && is_array($files)) {
             $data = array_replace_recursive($params, $files);
         } else {
             $data = $params ?: $files;
         }
     }
     // Don't auto-submit the form unless at least one field is present.
     if ('' === $name && count(array_intersect_key($data, $form->all())) <= 0) {
         return;
     }
     $form->submit($data, 'PATCH' !== $method);
 }
예제 #3
0
 public function preSubmit(FormEvent $event)
 {
     $form = $event->getForm();
     $postRequestSizeExceeded = $form->getConfig()->getMethod() === 'POST' && $this->serverParams->hasPostMaxSizeBeenExceeded();
     if ($form->isRoot() && $form->getConfig()->getOption('compound') && !$postRequestSizeExceeded) {
         $data = $event->getData();
         if (!isset($data[$this->fieldName]) || !$this->tokenManager->isTokenValid(new CsrfToken($this->tokenId, $data[$this->fieldName]))) {
             $errorMessage = $this->errorMessage;
             if (null !== $this->translator) {
                 $errorMessage = $this->translator->trans($errorMessage, array(), $this->translationDomain);
             }
             $form->addError(new FormError($errorMessage));
         }
         if (is_array($data)) {
             unset($data[$this->fieldName]);
             $event->setData($data);
         }
     }
 }
 /**
  * Mark the form with an error if the uploaded size was too large
  *
  * This is done here and not in FormValidator because $_POST is
  * empty when that error occurs. Hence the form is never submitted.
  *
  * @param  FormInterface $form
  * @return boolean
  */
 private function isUploadedSizeValid(FormInterface $form)
 {
     $contentLength = $this->serverParams->getContentLength();
     $maxContentLength = $this->serverParams->getPostMaxSize();
     if (!empty($maxContentLength) && $contentLength > $maxContentLength) {
         // Submit the form, but don't clear the default values
         $form->submit(null, false);
         $form->addError(new FormError($form->getConfig()->getOption('post_max_size_message'), null, array('{{ max }}' => $this->serverParams->getNormalizedIniPostMaxSize())));
         return false;
     }
     return true;
 }