Example #1
0
 /**
  * Adds a CSRF field to the root form view.
  *
  * @param FormView      $view The form view
  * @param FormInterface $form The form
  */
 public function buildView(FormView $view, FormInterface $form)
 {
     if ($form->isRoot() && $form->hasChildren() && $form->hasAttribute('csrf_field_name')) {
         $name = $form->getAttribute('csrf_field_name');
         $csrfProvider = $form->getAttribute('csrf_provider');
         $intention = $form->getAttribute('csrf_intention');
         $factory = $form->getAttribute('csrf_factory');
         $data = $csrfProvider->generateCsrfToken($intention);
         $csrfForm = $factory->createNamed('hidden', $name, $data, array('property_path' => false));
         $view->addChild($csrfForm->createView($view));
     }
 }
Example #2
0
 public static function getErrorMessages(FormInterface $form)
 {
     $errors = array();
     foreach ($form->getErrors() as $key => $error) {
         $template = $error->getMessageTemplate();
         $parameters = $error->getMessageParameters();
         foreach ($parameters as $var => $value) {
             $template = str_replace($var, $value, $template);
         }
         $errors[$key] = $template;
     }
     if ($form->hasChildren()) {
         foreach ($form->getChildren() as $child) {
             if (!$child->isValid()) {
                 $errors[$child->getName()] = self::getErrorMessages($child);
             }
         }
     }
     return $errors;
 }