Skip to content

Instantly share code, notes, and snippets.

@piotrplenik
Last active December 16, 2015 13:39
Show Gist options
  • 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. <[email protected]>
*
* 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