public function generateForm($params, $content, \Smarty_Internal_Template $template, &$repeat) { if ($repeat) { $name = $this->getParam($params, 'name'); $formType = $this->getParam($params, 'type', 'form'); if (null == $name) { $name = "thelia.empty"; } if (!isset($this->formDefinition[$name])) { throw new ElementNotFoundException(sprintf("%s form does not exists", $name)); } $formClass = $this->formDefinition[$name]; // Check if parser context contains our form $instance = $this->parserContext->getForm($name, $formClass, $formType); if (null === $instance) { // If not, create a new instance $instance = $this->formFactory->createForm($name); } // Set the current form $this->parserContext->pushCurrentForm($instance); $instance->createView(); $template->assign("form", $instance); $template->assign("form_name", $instance->getName()); $template->assign("form_error", $instance->hasError() ? true : false); $template->assign("form_error_message", $instance->getErrorMessage()); } else { $this->parserContext->popCurrentForm(); return $content; } }
/** * Check if the specified form has errors, and return an instance of this form if it's the case. * * @param string $formId the form ID, as defined in the container * @param string $formClass the form full qualified class name * @param string $formType the form type, something like 'form' * @return null|BaseForm null if no error information is available */ public function getForm($formId, $formClass, $formType) { if (isset($this->store[$formClass . ":" . $formType]) && $this->store[$formClass . ":" . $formType] instanceof BaseForm) { return $this->store[$formClass . ":" . $formType]; } $formErrorInformation = $this->getSession()->getFormErrorInformation(); if (isset($formErrorInformation[$formClass . ":" . $formType])) { $formInfo = $formErrorInformation[$formClass . ":" . $formType]; if (is_array($formInfo['data'])) { $form = $this->formFactory->createForm($formId, $formType, $formInfo['data'], ['validation_groups' => $formInfo['validation_groups']]); // If the form has errors, perform a validation, to restore the internal error context // A controller (as the NewsletterController) may use the parserContext to redisplay a // validated (not errored) form. In such cases, another validation may cause unexpected // results. if (true === $formInfo['hasError']) { try { $this->formValidator->validateForm($form, $formInfo['method']); } catch (\Exception $ex) { // Ignore the exception. } } $form->setError($formInfo['hasError']); // Check if error message is empty, as BaseForm::setErrorMessage() always set form error flag to true. if (!empty($formInfo['errorMessage'])) { $form->setErrorMessage($formInfo['errorMessage']); } return $form; } } return null; }