public function testGetContentLengthFromRequest()
 {
     $request = Request::create('http://foo', 'GET', array(), array(), array(), array('CONTENT_LENGTH' => 1024));
     $requestStack = $this->getMock('Symfony\\Component\\HttpFoundation\\RequestStack', array('getCurrentRequest'));
     $requestStack->expects($this->once())->method('getCurrentRequest')->will($this->returnValue($request));
     $serverParams = new ServerParams($requestStack);
     $this->assertEquals(1024, $serverParams->getContentLength());
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function validate($form, Constraint $constraint)
 {
     if (!$form instanceof FormInterface) {
         return;
     }
     /* @var FormInterface $form */
     $config = $form->getConfig();
     if ($form->isSynchronized()) {
         // Validate the form data only if transformation succeeded
         $path = $this->context->getPropertyPath();
         $graphWalker = $this->context->getGraphWalker();
         $groups = $this->getValidationGroups($form);
         if (!empty($path)) {
             $path .= '.';
         }
         // Validate the data against its own constraints
         if (self::allowDataWalking($form)) {
             foreach ($groups as $group) {
                 $graphWalker->walkReference($form->getData(), $group, $path . 'data', true);
             }
         }
         // Validate the data against the constraints defined
         // in the form
         $constraints = $config->getOption('constraints');
         foreach ($constraints as $constraint) {
             foreach ($groups as $group) {
                 if (in_array($group, $constraint->groups)) {
                     $graphWalker->walkConstraint($constraint, $form->getData(), $group, $path . 'data');
                     // Prevent duplicate validation
                     continue 2;
                 }
             }
         }
     } else {
         $clientDataAsString = is_scalar($form->getViewData()) ? (string) $form->getViewData() : gettype($form->getViewData());
         // Mark the form with an error if it is not synchronized
         $this->context->addViolation($config->getOption('invalid_message'), array('{{ value }}' => $clientDataAsString), $form->getViewData(), null, Form::ERR_INVALID);
     }
     // Mark the form with an error if it contains extra fields
     if (count($form->getExtraData()) > 0) {
         $this->context->addViolation($config->getOption('extra_fields_message'), array('{{ extra_fields }}' => implode('", "', array_keys($form->getExtraData()))), $form->getExtraData());
     }
     // Mark the form with an error if the uploaded size was too large
     $length = $this->serverParams->getContentLength();
     if ($form->isRoot() && null !== $length) {
         $max = $this->serverParams->getPostMaxSize();
         if (null !== $max && $length > $max) {
             $this->context->addViolation($config->getOption('post_max_size_message'), array('{{ max }}' => $this->serverParams->getNormalizedIniPostMaxSize()), $length);
         }
     }
 }
Example #3
0
 /**
  * {@inheritdoc}
  */
 public function validate($form, Constraint $constraint)
 {
     if (!$form instanceof FormInterface) {
         return;
     }
     /* @var FormInterface $form */
     $config = $form->getConfig();
     if ($form->isSynchronized()) {
         // Validate the form data only if transformation succeeded
         $groups = self::getValidationGroups($form);
         // Validate the data against its own constraints
         if (self::allowDataWalking($form)) {
             foreach ($groups as $group) {
                 $this->context->validate($form->getData(), 'data', $group, true);
             }
         }
         // Validate the data against the constraints defined
         // in the form
         $constraints = $config->getOption('constraints');
         foreach ($constraints as $constraint) {
             foreach ($groups as $group) {
                 if (in_array($group, $constraint->groups)) {
                     $this->context->validateValue($form->getData(), $constraint, 'data', $group);
                     // Prevent duplicate validation
                     continue 2;
                 }
             }
         }
     } else {
         $childrenSynchronized = true;
         foreach ($form as $child) {
             if (!$child->isSynchronized()) {
                 $childrenSynchronized = false;
                 break;
             }
         }
         // Mark the form with an error if it is not synchronized BUT all
         // of its children are synchronized. If any child is not
         // synchronized, an error is displayed there already and showing
         // a second error in its parent form is pointless, or worse, may
         // lead to duplicate errors if error bubbling is enabled on the
         // child.
         // See also https://github.com/symfony/symfony/issues/4359
         if ($childrenSynchronized) {
             $clientDataAsString = is_scalar($form->getViewData()) ? (string) $form->getViewData() : gettype($form->getViewData());
             $this->context->addViolation($config->getOption('invalid_message'), array_replace(array('{{ value }}' => $clientDataAsString), $config->getOption('invalid_message_parameters')), $form->getViewData(), null, Form::ERR_INVALID);
         }
     }
     // Mark the form with an error if it contains extra fields
     if (count($form->getExtraData()) > 0) {
         $this->context->addViolation($config->getOption('extra_fields_message'), array('{{ extra_fields }}' => implode('", "', array_keys($form->getExtraData()))), $form->getExtraData());
     }
     // Mark the form with an error if the uploaded size was too large
     $length = $this->serverParams->getContentLength();
     if ($form->isRoot() && null !== $length) {
         $max = $this->serverParams->getPostMaxSize();
         if (!empty($max) && $length > $max) {
             $this->context->addViolation($config->getOption('post_max_size_message'), array('{{ max }}' => $this->serverParams->getNormalizedIniPostMaxSize()), $length);
         }
     }
 }