Пример #1
0
 private function serializeForm(FormInterface $form)
 {
     if (!$form->all()) {
         return $form->getViewData();
     }
     $data = null;
     foreach ($form->all() as $child) {
         $name = $child->getConfig()->getName();
         $data[$name] = $this->serializeForm($child);
     }
     return $data;
 }
Пример #2
0
 /**
  * @dataProvider getValidInputs
  */
 public function testSubmit($submitted, $expected)
 {
     $this->sut->submit($submitted);
     $msg = '';
     if (!$this->sut->isValid()) {
         $msg = print_r($this->sut->getErrors(), true);
         foreach ($this->sut->all() as $child) {
             $msg .= "\n" . print_r($child->getErrors(), true);
         }
     }
     $this->assertTrue($this->sut->isValid(), $msg);
     $this->assertEquals($expected, $this->sut->getData());
 }
 public function interactWith(FormInterface $form, HelperSet $helperSet, InputInterface $input, OutputInterface $output)
 {
     if (!$form->isRoot()) {
         throw new CanNotInteractWithForm('This interactor only works with root forms');
     }
     if ($input->isInteractive()) {
         throw new CanNotInteractWithForm('This interactor only works with non-interactive input');
     }
     /*
      * We need to adjust the input values for repeated types by copying the provided value to both of the repeated
      * fields. We only loop through the top-level fields, since there are no command options for deeper lying fields
      * anyway.
      *
      * The fix was provided by @craigh
      *
      * P.S. If we need to add another fix like this, we should move this out to dedicated "input fixer" classes.
      */
     foreach ($form->all() as $child) {
         $config = $child->getConfig();
         $name = $child->getName();
         if ($config->getType()->getInnerType() instanceof RepeatedType && $input->hasOption($name)) {
             $input->setOption($name, [$config->getOption('first_name') => $input->getOption($name), $config->getOption('second_name') => $input->getOption($name)]);
         }
     }
     // use the original input as the submitted data
     return $input;
 }
 /**
  * Merges constraints from the form with constraints in the class metaData
  *
  * @param FormInterface $form
  */
 public function mergeConstraints(FormInterface &$form)
 {
     $metaData = null;
     $dataClass = $form->getConfig()->getDataClass();
     if ($dataClass != '') {
         $metaData = $this->validator->getMetadataFor($dataClass);
     }
     if ($metaData instanceof ClassMetadata) {
         /**
          * @var FormInterface $child
          */
         foreach ($form->all() as $child) {
             $options = $child->getConfig()->getOptions();
             $name = $child->getConfig()->getName();
             $type = $child->getConfig()->getType()->getName();
             if (isset($options['constraints'])) {
                 $existingConstraints = $options['constraints'];
                 $extractedConstraints = $this->extractPropertyConstraints($name, $metaData);
                 // Merge all constraints
                 $options['constraints'] = array_merge($existingConstraints, $extractedConstraints);
             }
             $form->add($name, $type, $options);
         }
     }
 }
Пример #5
0
 public function transform(FormInterface $form, $extensions = [])
 {
     $data = [];
     $order = 1;
     $required = [];
     foreach ($form->all() as $name => $field) {
         $transformedChild = $this->resolver->resolve($field)->transform($field, $extensions);
         $transformedChild['propertyOrder'] = $order;
         $data[$name] = $transformedChild;
         $order++;
         if ($this->resolver->resolve($field)->isRequired($field)) {
             $required[] = $field->getName();
         }
     }
     $schema = ['title' => $form->getConfig()->getOption('label'), 'type' => 'object', 'properties' => $data];
     if (!empty($required)) {
         $schema['required'] = $required;
     }
     $innerType = $form->getConfig()->getType()->getInnerType();
     if (method_exists($innerType, 'buildLiform')) {
         $schema['liform'] = $innerType->buildLiform($form);
     }
     $this->addCommonSpecs($form, $schema, $extensions);
     return $schema;
 }
Пример #6
0
 /**
  * @param FormInterface     $form
  * @param array             $results
  *
  * @return array
  */
 private function formatFormErrors(FormInterface $form, &$results = array())
 {
     /*
      * first check if there are any errors bound for this element
      */
     $errors = $form->getErrors();
     if (count($errors)) {
         $messages = [];
         foreach ($errors as $error) {
             if ($error instanceof FormError) {
                 $messages[] = $this->translator->trans($error->getMessage(), $error->getMessageParameters(), 'validators');
             }
             $results[] = ['property_path' => $this->formatPropertyPath($error), 'message' => join(', ', $messages)];
         }
     }
     /*
      * Then, check if there are any children. If yes, then parse them
      */
     $children = $form->all();
     if (count($children)) {
         foreach ($children as $child) {
             if ($child instanceof FormInterface) {
                 $this->formatFormErrors($child, $results);
             }
         }
     }
     return $results;
 }
Пример #7
0
 /**
  * {@inheritDoc}
  */
 public function buildView(FormView $view, FormInterface $form, array $options)
 {
     $children = $form->all();
     $view->vars['value']['type'] = $children['type']->getViewData();
     $view->vars['value']['value'] = $children['value']->getViewData();
     $view->vars['show_filter'] = $options['show_filter'];
 }
Пример #8
0
 /**
  * {@inheritdoc}
  *
  * @param FormView      $view
  * @param FormInterface $form
  * @param array         $options
  */
 public function buildView(FormView $view, FormInterface $form, array $options)
 {
     if ($form->count() == 0) {
         return;
     }
     array_map(array($this, 'validateButton'), $form->all());
 }
Пример #9
0
 /**
  * Returns an array with form fields errors
  *
  * @param FormInterface $form
  * @param bool|false $useLabels
  * @param array $errors
  * @return array
  */
 public function getErrorMessages(FormInterface $form, $useLabels = false, $errors = array())
 {
     if ($form->count() > 0) {
         foreach ($form->all() as $child) {
             if (!$child->isValid()) {
                 $errors = $this->getErrorMessages($child, $useLabels, $errors);
             }
         }
     }
     foreach ($form->getErrors() as $error) {
         if ($useLabels) {
             $fieldNameData = $this->getErrorFormLabel($form);
         } else {
             $fieldNameData = $this->getErrorFormId($form);
         }
         $fieldName = $fieldNameData;
         if ($useLabels) {
             /**
              * @ignore
              */
             $fieldName = $this->translator->trans($fieldNameData['label'], array(), $fieldNameData['domain']);
         }
         $errors[$fieldName] = $error->getMessage();
     }
     return $errors;
 }
 public function finishView(FormView $view, FormInterface $form, array $options)
 {
     foreach ($form->all() as $identifier => $child) {
         if (false === $child->getConfig()->getOption('serialize_only')) {
             continue;
         }
         unset($view->children[$identifier]);
     }
 }
Пример #11
0
 public function __construct(FormInterface $form)
 {
     $this->name = $form->getName();
     foreach ($form->getErrors() as $error) {
         $this->errors[] = $error;
     }
     foreach ($form->all() as $child) {
         $this->children[] = new static($child);
     }
 }
 /**
  * @param InputInterface $input
  * @param FormInterface  $form
  *
  * @return array
  */
 private function convertInputToSubmittedData(InputInterface $input, FormInterface $form)
 {
     $submittedData = [];
     // we don't need to do this recursively, since command options are one-dimensional (or are they?)
     foreach ($form->all() as $name => $field) {
         if ($input->hasOption($name)) {
             $submittedData[$name] = $input->getOption($name);
         }
     }
     return $submittedData;
 }
 /**
  * @param FormInterface $form
  * @return array
  */
 protected function getFormChildErrorMessages(FormInterface $form)
 {
     $errors = [];
     foreach ($form->all() as $child) {
         if (!empty($child) && !$child->isValid()) {
             foreach ($this->getErrorMessages($child) as $error) {
                 $errors[] = $error;
             }
         }
     }
     return $errors;
 }
 /**
  * @param FormInterface $form
  * @return array
  */
 protected function getFormChildErrorMessages(FormInterface $form)
 {
     $errors = [];
     foreach ($form->all() as $child) {
         if ($this->shouldAddChildErrorMessage($child)) {
             foreach ($this->getErrorMessages($child) as $error) {
                 $errors[] = $error;
             }
         }
     }
     return $errors;
 }
Пример #15
0
 /**
  * @param mixed         $data
  * @param FormInterface $form
  * 
  * @return mixed
  * 
  * @throws \InvalidArgumentException
  */
 public function getFullAddress($data, FormInterface $form)
 {
     $fields = array();
     foreach ($form->all() as $field) {
         $options = $field->getConfig()->getOptions();
         if (isset($options['geo_code_field']) && true == $options['geo_code_field']) {
             $fields[] = $data[$field->getName()];
         }
     }
     if (count($fields)) {
         return implode(' ', $fields);
     }
     throw new \InvalidArgumentException('GeoDataAdapter address field mismatch');
 }
Пример #16
0
 /**
  * @param  FormInterface $form
  * @return string        All form's error messages concatenated into one string
  */
 protected function getFormErrors(FormInterface $form)
 {
     $errors = '';
     /** @var FormError $error */
     foreach ($form->getErrors() as $error) {
         $errors .= $error->getMessage() . "\n";
     }
     foreach ($form->all() as $key => $child) {
         if ($err = $this->getFormErrors($child)) {
             $errors .= sprintf("%s: %s\n", $key, $err);
         }
     }
     return $errors;
 }
Пример #17
0
 /**
  * Helper for extracting errors from form child elements
  */
 public function getFormErrors(FormInterface $form)
 {
     $errors = array();
     foreach ($form->all() as $field) {
         $childErrors = $this->getFormErrors($field);
         if ($childErrors) {
             $errors[$field->getName()] = $childErrors;
         }
     }
     foreach ($form->getErrors() as $error) {
         $errors['errors'] = $error->getMessage();
     }
     return $errors;
 }
Пример #18
0
 /**
  * Helper for extracting errors from form child elements
  */
 public static function getErrors(FormInterface $form)
 {
     $errors = array();
     foreach ($form->all() as $field) {
         $childErrors = self::getErrors($field);
         if ($childErrors) {
             $errors[$form->getName() . '_' . $field->getName()][] = $childErrors;
         }
     }
     foreach ($form->getErrors() as $error) {
         $errors[] = $error->getMessage();
     }
     return $errors;
 }
Пример #19
0
 /**
  * @param FormInterface $form
  * @return \Symfony\Component\Form\FormError[]
  */
 protected function getAllErrors(FormInterface $form)
 {
     $errors = $form->getErrors();
     $children = $form->all();
     if (!count($errors) && !count($children)) {
         return null;
     }
     $section = ['field' => $form->getName()];
     if (count($errors)) {
         $section['errors'] = array_map(function (FormError $error) {
             return ['message' => $error->getMessage(), 'parameters' => $error->getMessageParameters()];
         }, iterator_to_array($form->getErrors()));
     }
     if (count($children)) {
         $section['children'] = array_values(array_filter(array_map(function (FormInterface $field) {
             return $this->getAllErrors($field);
         }, $form->all())));
         if (!count($errors) && !count($section['children'])) {
             return null;
         }
     }
     return $section;
 }
 public function formErrorsToArray(FormInterface $form)
 {
     $errors = array();
     foreach ($form->getErrors() as $error) {
         $errors[] = $error->getMessage();
     }
     /* @var $child \Symfony\Component\Form\Form */
     foreach ($form->all() as $child) {
         if (!$child->isValid()) {
             $errors[$child->getName()] = $this->formErrorsToArray($child);
         }
     }
     return $errors;
 }
 /**
  * {@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;
     }
     if ('GET' === $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 {
         $fixedFiles = array();
         foreach ($_FILES as $name => $file) {
             $fixedFiles[$name] = 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);
 }
Пример #22
0
 /**
  * Collects errors from form
  *
  * @param FormInterface $form
  * @return array
  */
 public function getErrorsFromForm(FormInterface $form)
 {
     $errors = array();
     foreach ($form->getErrors() as $error) {
         $errors[] = $error->getMessage();
     }
     foreach ($form->all() as $childForm) {
         if ($childForm instanceof FormInterface) {
             if ($childErrors = $this->getErrorsFromForm($childForm)) {
                 $errors[$childForm->getName()] = $childErrors;
             }
         }
     }
     return $errors;
 }
 /**
  * Returns the form's data like $form->submit() expects it
  *
  * @param FormInterface $form
  * @return array
  */
 protected function unbind(FormInterface $form)
 {
     if ($form->count() > 0) {
         $ary = array();
         foreach ($form->all() as $name => $child) {
             $value = $this->unbind($child);
             if (null !== $value || (is_array($value) || $value instanceof Collection) && count($value) > 0) {
                 $ary[$name] = $value;
             }
         }
         return $ary;
     } else {
         $data = $form->getViewData();
         return $data instanceof Collection ? $data->toArray() : $data;
     }
 }
Пример #24
0
 function it_does_not_handle_form_when_the_form_is_invalid(Request $request, FormFactoryInterface $formFactory, FormBuilderInterface $formBuilder, FormInterface $form, FormError $formError, FormInterface $child)
 {
     $formFactory->createNamedBuilder('', 'kreta_dummy_type', null, [])->shouldBeCalled()->willReturn($formBuilder);
     $formBuilder->getForm()->shouldBeCalled()->willReturn($form);
     $request->isMethod('POST')->shouldBeCalled()->willReturn(true);
     $form->handleRequest($request)->shouldBeCalled()->willReturn($form);
     $form->isValid()->shouldBeCalled()->willReturn(false);
     $form->getErrors()->shouldBeCalled()->willReturn([$formError]);
     $formError->getMessage()->shouldBeCalled()->willReturn('Form error');
     $form->all()->shouldBeCalled()->willReturn([$child]);
     $child->isValid()->shouldBeCalled()->willReturn(false);
     $child->getName()->shouldBeCalled()->willReturn('Child form error');
     $child->getErrors()->shouldBeCalled()->willReturn([]);
     $child->all()->shouldBeCalled()->willReturn([]);
     $this->shouldThrow(new InvalidFormException(['Form error', 'Child form error' => []]))->during('handleForm', [$request]);
 }
Пример #25
0
 /**
  * @param FormInterface $form
  * @return array
  */
 protected function getErrorMessages(FormInterface $form)
 {
     $errors = array();
     foreach ($form->getErrors() as $key => $error) {
         $errors[] = $this->translator->trans($error->getMessage());
     }
     foreach ($form->all() as $child) {
         if (!$child->isValid()) {
             $childErrors = $this->getErrorMessages($child);
             foreach ($childErrors as $childError) {
                 $errors[] = $childError;
             }
         }
     }
     return $errors;
 }
Пример #26
0
 /**
  * Collect form errors.
  *
  * @param FormInterface $form
  *
  * @return array
  */
 protected function getFormErrors(FormInterface $form)
 {
     $errors = [];
     foreach ($form->getErrors() as $error) {
         $errors[] = $error->getMessage();
     }
     foreach ($form->all() as $childForm) {
         if ($childForm instanceof FormInterface) {
             $childErrors = $this->getFormErrors($childForm);
             if ($childErrors) {
                 $errors[$childForm->getName()] = $childErrors;
             }
         }
     }
     return $errors;
 }
 /**
  * Gets all the errors in the current $field and adds them to $_errors, to
  * get all errors in the form and its children recursively.
  * Also adds display name to error-message.
  *
  * @param  FormInterface $field Field to get Errors from
  */
 protected function _getErrors(FormInterface $field)
 {
     if ($field->getConfig()->getOption('label')) {
         $label = $this->_translator->trans($field->getConfig()->getOption('label'));
     } else {
         $label = ucfirst(trim(strtolower(preg_replace(array('/([A-Z])/', '/[_\\s]+/'), array('_$1', ' '), $field->getName()))));
     }
     $fieldErrors = array();
     foreach ($field->getErrors() as $error) {
         // don't show label if error is on the whole form
         $fieldErrors[] = $field->isRoot() ? $error->getMessage() : $label . ': ' . $error->getMessage();
     }
     $this->_errors[] = $fieldErrors;
     foreach ($field->all() as $child) {
         $this->_getErrors($child);
     }
 }
Пример #28
0
 public function transform(FormInterface $form, $extensions = [])
 {
     $children = [];
     //$entryType = $form->getConfig()->getAttribute('prototype');
     //$children[] = $this->resolver->resolve($entryType)->transform($entryType, $extensions);
     //$children[0]['title'] = 'prototype';
     foreach ($form->all() as $name => $field) {
         $transformedChild = $this->resolver->resolve($field)->transform($field, $extensions);
         $children[] = $transformedChild;
         if ($this->resolver->resolve($field)->isRequired($field)) {
             $required[] = $field->getName();
         }
     }
     $schema = ['type' => 'array', 'title' => $form->getConfig()->getOption('label'), 'items' => $children];
     $this->addCommonSpecs($form, $schema, $extensions);
     return $schema;
 }
 /**
  * {@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;
     }
     if ('GET' === $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 {
         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);
 }
Пример #30
0
 protected function getErrorsFromForm(FormInterface $form)
 {
     $errors = array();
     foreach ($form->getErrors() as $error) {
         if ($error->getCause()) {
             $errors[$error->getCause()->getPropertyPath()] = $error->getMessage();
         } else {
             $errors[] = $error->getMessage();
         }
     }
     foreach ($form->all() as $childForm) {
         if ($childForm instanceof FormInterface) {
             if ($childErrors = $this->getErrorsFromForm($childForm)) {
                 $errors[$childForm->getName()] = $childErrors;
             }
         }
     }
     return $errors;
 }