diff --git a/src/Symfony/Component/Form/Extension/Core/Type/FieldType.php b/src/Symfony/Component/Form/Extension/Core/Type/FieldType.php index 8b4910805bec..bef85578e34f 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/FieldType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/FieldType.php @@ -70,7 +70,37 @@ public function buildForm(FormBuilder $builder, array $options) */ public function buildView(FormView $view, FormInterface $form) { - $name = $form->getName(); + $types = array_map(function($type) { + return $type->getName(); + }, $form->getTypes()); + + if (in_array('radio', $types) || in_array('checkbox', $types)) { + + $slugs = array(); + + foreach (array_keys($view->getParent()->get('choices')) as $value) { + $slug = $this->slugify($value); + $slugs[$slug][] = $value; + } + + $slug = $this->slugify($form->getName()); + + // trick to make the slug unique + if (count($slugs[$slug]) > 1) { + $index = array_search($form->getName(), $slugs[$slug]); + $slug.= ' '.$index; + } + + } else { + $slug = $this->slugify($form->getName()); + } + + // this is used to keep the 1st underscore for _token but i guess it + // would be better to name it "-token" to keep things consistent + if ($slug{0} === '-') { + $slug{0} = '_'; + } + $name = $slug; if ($view->hasParent()) { $parentId = $view->getParent()->get('id'); @@ -82,11 +112,6 @@ public function buildView(FormView $view, FormInterface $form) $fullName = $name; } - $types = array(); - foreach ($form->getTypes() as $type) { - $types[] = $type->getName(); - } - $view ->set('form', $view) ->set('id', $id) @@ -176,4 +201,14 @@ private function humanize($text) { return ucfirst(strtolower(str_replace('_', ' ', $text))); } + + private function slugify($str, $delimiter = '-') + { + $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str); + $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean); + $clean = strtolower(trim($clean, '-')); + $clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean); + + return rtrim($clean, $delimiter); + } }