Przeglądaj źródła

[Form] added a form type name validator

Fabien Potencier 14 lat temu
rodzic
commit
7eec2ca7b3
1 zmienionych plików z 11 dodań i 0 usunięć
  1. 11 0
      src/Symfony/Component/Form/FormFactory.php

+ 11 - 0
src/Symfony/Component/Form/FormFactory.php

@@ -91,6 +91,8 @@ class FormFactory implements FormFactoryInterface
     {
         $this->loadTypeExtensions($type);
 
+        $this->validateFormTypeName($type);
+
         $this->types[$type->getName()] = $type;
     }
 
@@ -377,6 +379,8 @@ class FormFactory implements FormFactoryInterface
 
         $this->loadTypeExtensions($type);
 
+        $this->validateFormTypeName($type);
+
         $this->types[$name] = $type;
     }
 
@@ -398,4 +402,11 @@ class FormFactory implements FormFactoryInterface
 
         $type->setExtensions($typeExtensions);
     }
+
+    private function validateFormTypeName(FormTypeInterface $type)
+    {
+        if (!preg_match('/^[a-z0-9_]+$/i', $type->getName())) {
+            throw new FormException(sprintf('The "%s" form type name ("%s") is not valid. Names must only contain letters, numbers, and "_".', get_class($type), $type->getName()));
+        }
+    }
 }