/** * Validate a BaseForm * * @param BaseForm $aBaseForm the form * @param string $expectedMethod the expected method, POST or GET, or null for any of them * @throws FormValidationException is the form contains error, or the method is not the right one * @return \Symfony\Component\Form\Form Form the symfony form object */ public function validateForm(BaseForm $aBaseForm, $expectedMethod = null) { $form = $aBaseForm->getForm(); if ($expectedMethod == null || $aBaseForm->getRequest()->isMethod($expectedMethod)) { $form->handleRequest($aBaseForm->getRequest()); if ($form->isValid()) { if ($aBaseForm instanceof FirewallForm && !$aBaseForm->isFirewallOk($this->environment)) { throw new FormValidationException($this->translator->trans("You've submitted this form too many times. " . "Further submissions will be ignored during %time", ["%time" => $aBaseForm->getWaitingTime()])); } return $form; } else { $errorMessage = null; if ($form->get("error_message")->getData() != null) { $errorMessage = $form->get("error_message")->getData(); } else { $errorMessage = sprintf($this->translator->trans("Missing or invalid data: %s"), $this->getErrorMessages($form)); } throw new FormValidationException($errorMessage); } } else { throw new FormValidationException(sprintf($this->translator->trans("Wrong form method, %s expected."), $expectedMethod)); } }
/** * @param $collection * @param BaseForm $form * @return SymfonyForm * * Extract the collection object from the form */ protected function resolveCollection($collection, BaseForm $form) { if (null === $collection) { throw new \InvalidArgumentException("Missing parameter 'collection' in 'form_collection"); } $sfForm = $form->getForm(); if (!$sfForm->has($collection)) { throw new \InvalidArgumentException(sprintf("Field name '%s' not found in form %s children", $collection, $form->getName())); } /** * Check that the field is a "collection" type */ $collectionConfig = $this->retrieveField($collection, $sfForm->all(), $form->getName()); $fieldType = $collectionConfig->getConfig()->getType(); if ($fieldType->getName() !== static::COLLECTION_TYPE_NAME) { $baseFieldType = $fieldType; $resolved = false; while (null !== $fieldType && !$resolved) { if ($fieldType->getName() !== static::COLLECTION_TYPE_NAME) { $fieldType = $fieldType->getParent(); } } if (!$resolved) { throw new \LogicException(sprintf("The field '%s' is not a collection, it's a '%s'." . "You can't use it with the function 'form_collection' in form '%s'", $collection, $baseFieldType->getName(), $form->getName())); } } return $collectionConfig; }