Skip to content

Instantly share code, notes, and snippets.

@piotrplenik
Last active December 16, 2015 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piotrplenik/5443225 to your computer and use it in GitHub Desktop.
Save piotrplenik/5443225 to your computer and use it in GitHub Desktop.
sfWidgetFormSelectRadioSingleable Symfony widget lets you render just one option at a time. Extends sfWidgetFormSelectRadio. Inspiration from https://gist.github.com/ain/4165050
<?php
/**
* This file is part of the MnumiPrint package.
*
* (c) Mnumi Sp. z o.o. <mnumi@mnumi.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class sfWidgetFormSelectRadioSingleable extends sfWidgetFormSelectRadio
{
public function formatChoices($name, $value, $choices, $attributes)
{
$onlyChoice = !empty($attributes['only_choice']) ? $attributes['only_choice'] : false;
unset($attributes['only_choice']);
if ($onlyChoice)
{
$option = $choices[$onlyChoice];
return parent::formatChoices($name, $value, array($onlyChoice => $option), $attributes);
}
return parent::formatChoices($name, $value, $choices, $attributes);
}
public function generateId($name, $value = null)
{
if($value == 0)
{
$value = time().rand(1, 50);
}
return parent::generateId($name, $value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment