[Form] Enable empty root form name
BC Break: no Feature addition: yes Symfony2 tests pass: yes Fixes the following issues: #2790 Todo: need more testing This PR enables usage of empty string as a form name (only at root level).
This commit is contained in:
parent
c637869c95
commit
3d20e399e9
@ -74,10 +74,15 @@ class FieldType extends AbstractType
|
||||
$name = $form->getName();
|
||||
|
||||
if ($view->hasParent()) {
|
||||
$parentId = $view->getParent()->get('id');
|
||||
$parentFullName = $view->getParent()->get('full_name');
|
||||
$id = sprintf('%s_%s', $parentId, $name);
|
||||
$fullName = sprintf('%s[%s]', $parentFullName, $name);
|
||||
if ('' === $name) {
|
||||
throw new FormException('Form node with empty name can be used only as root form node.');
|
||||
} elseif ('' !== ($parentFullName = $view->getParent()->get('full_name'))) {
|
||||
$id = sprintf('%s_%s', $view->getParent()->get('id'), $name);
|
||||
$fullName = sprintf('%s[%s]', $parentFullName, $name);
|
||||
} else {
|
||||
$id = $name;
|
||||
$fullName = $name;
|
||||
}
|
||||
} else {
|
||||
$id = $name;
|
||||
$fullName = $name;
|
||||
|
@ -577,13 +577,20 @@ class Form implements \IteratorAggregate, FormInterface
|
||||
switch ($request->getMethod()) {
|
||||
case 'POST':
|
||||
case 'PUT':
|
||||
$data = array_replace_recursive(
|
||||
$request->request->get($this->getName(), array()),
|
||||
$request->files->get($this->getName(), array())
|
||||
);
|
||||
if ('' === $this->getName()) {
|
||||
$data = array_replace_recursive(
|
||||
$request->request->all(),
|
||||
$request->files->all()
|
||||
);
|
||||
} else {
|
||||
$data = array_replace_recursive(
|
||||
$request->request->get($this->getName(), array()),
|
||||
$request->files->get($this->getName(), array())
|
||||
);
|
||||
}
|
||||
break;
|
||||
case 'GET':
|
||||
$data = $request->query->get($this->getName(), array());
|
||||
$data = '' === $this->getName() ? $request->query->all() : $request->query->get($this->getName(), array());
|
||||
break;
|
||||
default:
|
||||
throw new FormException(sprintf('The request method "%s" is not supported', $request->getMethod()));
|
||||
|
@ -411,7 +411,7 @@ class FormFactory implements FormFactoryInterface
|
||||
|
||||
private function validateFormTypeName(FormTypeInterface $type)
|
||||
{
|
||||
if (!preg_match('/^[a-z0-9_]+$/i', $type->getName())) {
|
||||
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()));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user