Beispiel #1
0
    public function validate(FormInterface $form)
    {
        if (!$form->isSynchronized()) {
            $form->addError(new FormError('The value is invalid'));
        }

        if (count($form->getExtraData()) > 0) {
            $form->addError(new FormError('This form should not contain extra fields'));
        }

        if ($form->isRoot() && isset($_SERVER['CONTENT_LENGTH'])) {
            $length = (int) $_SERVER['CONTENT_LENGTH'];
            $max = trim(ini_get('post_max_size'));

            switch (strtolower(substr($max, -1))) {
                // The 'G' modifier is available since PHP 5.1.0
                case 'g':
                    $max *= 1024;
                case 'm':
                    $max *= 1024;
                case 'k':
                    $max *= 1024;
            }

            if ($length > $max) {
                $form->addError(new FormError('The uploaded file was too large. Please try to upload a smaller file'));
            }
        }
    }
 /**
  *
  * @param FormInterface $form
  * @param array $formData
  */
 public function commonTest(FormInterface $form, $formData)
 {
     $this->assertTrue($form->isSynchronized());
     $view = $form->createView();
     $children = $view->children;
     foreach (array_keys($formData) as $key) {
         $this->assertArrayHasKey($key, $children);
     }
 }
 public function mapFormToData(FormInterface $form, &$data)
 {
     if ($form->getAttribute('property_path') !== null && $form->isSynchronized()) {
         $propertyPath = $form->getAttribute('property_path');
         // If the data is identical to the value in $data, we are
         // dealing with a reference
         $isReference = $form->getData() === $propertyPath->getValue($data);
         $byReference = $form->getAttribute('by_reference');
         if (!(is_object($data) && $isReference && $byReference)) {
             $propertyPath->setValue($data, $form->getData());
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function extractSubmittedData(FormInterface $form)
 {
     $data = array('submitted_data' => array('norm' => $this->valueExporter->exportValue($form->getNormData())), 'errors' => array());
     if ($form->getViewData() !== $form->getNormData()) {
         $data['submitted_data']['view'] = $this->valueExporter->exportValue($form->getViewData());
     }
     if ($form->getData() !== $form->getNormData()) {
         $data['submitted_data']['model'] = $this->valueExporter->exportValue($form->getData());
     }
     foreach ($form->getErrors() as $error) {
         $data['errors'][] = array('message' => $error->getMessage());
     }
     $data['synchronized'] = $this->valueExporter->exportValue($form->isSynchronized());
     return $data;
 }
 /**
  * {@inheritdoc}
  */
 public function extractSubmittedData(FormInterface $form)
 {
     $data = array('submitted_data' => array('norm' => $this->valueExporter->exportValue($form->getNormData())), 'errors' => array());
     if ($form->getViewData() !== $form->getNormData()) {
         $data['submitted_data']['view'] = $this->valueExporter->exportValue($form->getViewData());
     }
     if ($form->getData() !== $form->getNormData()) {
         $data['submitted_data']['model'] = $this->valueExporter->exportValue($form->getData());
     }
     foreach ($form->getErrors() as $error) {
         $errorData = array('message' => $error->getMessage(), 'origin' => is_object($error->getOrigin()) ? spl_object_hash($error->getOrigin()) : null, 'trace' => array());
         $cause = $error->getCause();
         while (null !== $cause) {
             if ($cause instanceof ConstraintViolationInterface) {
                 $errorData['trace'][] = array('class' => $this->valueExporter->exportValue(get_class($cause)), 'root' => $this->valueExporter->exportValue($cause->getRoot()), 'path' => $this->valueExporter->exportValue($cause->getPropertyPath()), 'value' => $this->valueExporter->exportValue($cause->getInvalidValue()));
                 $cause = method_exists($cause, 'getCause') ? $cause->getCause() : null;
                 continue;
             }
             if ($cause instanceof \Exception) {
                 $errorData['trace'][] = array('class' => $this->valueExporter->exportValue(get_class($cause)), 'message' => $this->valueExporter->exportValue($cause->getMessage()));
                 $cause = $cause->getPrevious();
                 continue;
             }
             $errorData['trace'][] = $cause;
             break;
         }
         $data['errors'][] = $errorData;
     }
     $data['synchronized'] = $this->valueExporter->exportValue($form->isSynchronized());
     return $data;
 }
 /**
  * @param FormInterface $form
  *
  * @return Boolean
  */
 private function acceptsErrors(FormInterface $form)
 {
     return $this->allowNonSynchronized || $form->isSynchronized();
 }
Beispiel #7
0
 /**
  * @param FormInterface $form
  *
  * @return bool
  */
 private function acceptsErrors(FormInterface $form)
 {
     // Ignore non-submitted forms. This happens, for example, in PATCH
     // requests.
     // https://github.com/symfony/symfony/pull/10567
     return $form->isSubmitted() && ($this->allowNonSynchronized || $form->isSynchronized());
 }
Beispiel #8
0
 /**
  * @return Boolean
  */
 private function isValidScope()
 {
     return $this->allowNonSynchronized || $this->scope->isSynchronized();
 }
 /**
  * {@inheritdoc}
  */
 public function extractSubmittedData(FormInterface $form)
 {
     $data = array('submitted_data' => array('norm' => $this->valueExporter->exportValue($form->getNormData())), 'errors' => array());
     if ($form->getViewData() !== $form->getNormData()) {
         $data['submitted_data']['view'] = $this->valueExporter->exportValue($form->getViewData());
     }
     if ($form->getData() !== $form->getNormData()) {
         $data['submitted_data']['model'] = $this->valueExporter->exportValue($form->getData());
     }
     foreach ($form->getErrors() as $error) {
         $errorData = array('message' => $error->getMessage(), 'origin' => is_object($error->getOrigin()) ? spl_object_hash($error->getOrigin()) : null);
         $cause = $error->getCause();
         if ($cause instanceof ConstraintViolationInterface) {
             $errorData['cause'] = array('root' => $this->valueExporter->exportValue($cause->getRoot()), 'path' => $this->valueExporter->exportValue($cause->getPropertyPath()), 'value' => $this->valueExporter->exportValue($cause->getInvalidValue()));
         } else {
             $errorData['cause'] = null !== $cause ? $this->valueExporter->exportValue($cause) : null;
         }
         $data['errors'][] = $errorData;
     }
     $data['synchronized'] = $this->valueExporter->exportValue($form->isSynchronized());
     return $data;
 }