-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] choices ids are malformed #1919
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Several problems here:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about using the Inflector from Lithium? i believe it does all this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inflection is very different from slugification and does not solve the same problem. I've just had a quick look at the Inflector class in Lithium and the transliteration part is just plain wrong. |
||
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean); | ||
$clean = strtolower(trim($clean, '-')); | ||
$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why removing the underscores ? they are valid AFAIK. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just to keep a single delimiter There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but this is what force you to use a hack for the CSRF token field. And the form framework already uses underscores as delimiter in many places. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know that underscore is used a lot but with the use of the dash as a delimiter, it emphasizes the hierarchy in the form's ids wich is usefull when writing js for example
This is a silly example but I'm sure you understand what I'm trying to explain |
||
|
||
return rtrim($clean, $delimiter); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would have kept the
for
loop as done in the original code (it's faster).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about
array_map('strval', $form->getTypes());
is that also slow? (would simplify the for or foreach loop)