Skip to content

Commit

Permalink
Merge pull request #21 from LExpress/fix-array-widget
Browse files Browse the repository at this point in the history
[widget] Fix array to string convertion when using renderId with an array as value
  • Loading branch information
GromNaN committed Oct 3, 2013
2 parents 5f1f750 + edfe997 commit 2c447b5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/widget/sfWidgetForm.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public function generateId($name, $value = null)
// check to see if we have an array variable for a field name
if (strstr($name, '['))
{
$name = str_replace(array('[]', '][', '[', ']'), array((null !== $value ? '_'.$value : ''), '_', '_', ''), $name);
$name = str_replace(array('[]', '][', '[', ']'), array((null !== $value && !is_array($value) ? '_'.$value : ''), '_', '_', ''), $name);
}

if (false !== strpos($this->getOption('id_format'), '%s'))
Expand Down Expand Up @@ -330,7 +330,7 @@ protected function translate($text, array $parameters = array())
* @param array $parameters The values to replace the placeholders
*
* @return array The translated texts
*
*
* @see sfWidgetFormSchemaFormatter::translate()
*/
protected function translateAll(array $texts, array $parameters = array())
Expand Down
11 changes: 6 additions & 5 deletions test/unit/widget/sfWidgetFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

require_once(dirname(__FILE__).'/../../bootstrap/unit.php');

$t = new lime_test(34);
$t = new lime_test(35);

class MyWidgetForm extends sfWidgetForm
{
Expand Down Expand Up @@ -106,12 +106,13 @@ public function generateId($name, $value = null)
$t->is($w->generateId('foo[]'), 'id_for_foo_works', '->generateId() removes the [] from the name');
$t->is($w->generateId('foo[bar][]'), 'id_for_foo_bar_works', '->generateId() replaces [] with _');
$t->is($w->generateId('foo[bar][]', 'test'), 'id_for_foo_bar_test_works', '->generateId() takes the value into account if provided');
$t->is($w->generateId('_foo[bar][]', 'test'), 'id_for__foo_bar_test_works', '->generateId() leaves valid ids');
$t->is($w->generateId('_foo[bar][]', 'test'), 'id_for__foo_bar_test_works', '->generateId() leaves valid ids');

$w->setIdFormat('id');
$t->is($w->generateId('foo[bar][]', 'test'), 'foo_bar_test', '->generateId() returns the name if the id format does not contain %s');
$t->is($w->generateId('foo[bar][]', array('test1', 'test2')), 'foo_bar', '->generateId() ignore the value if not a string');

$w->setIdFormat('%s');
$t->is($w->generateId('_foo[bar][]', 'test'), 'foo_bar_test', '->generateId() removes invalid characters');
$t->is($w->generateId('_foo@bar'), 'foo_bar', '->generateId() removes invalid characters');
$t->is($w->generateId('_____foo@bar'), 'foo_bar', '->generateId() removes invalid characters');
$t->is($w->generateId('_foo[bar][]', 'test'), 'foo_bar_test', '->generateId() removes invalid characters');
$t->is($w->generateId('_foo@bar'), 'foo_bar', '->generateId() removes invalid characters');
$t->is($w->generateId('_____foo@bar'), 'foo_bar', '->generateId() removes invalid characters');

0 comments on commit 2c447b5

Please sign in to comment.