I'm using DoctrineEnumBundle from Fresh and I have this type defined:
namespace CommonBundle\DBAL\Types;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Fresh\Bundle\DoctrineEnumBundle\DBAL\Types\AbstractEnumType;
class AdminRoleType extends AbstractEnumType
{
const ROLE_ADMIN = "ROLE_ADMIN";
const ROLE_GERENTE = "ROLE_GERENTE";
const ROLE_FISCAL = "ROLE_FISCAL";
const ROLE_CENTRO_HIPICO = "ROLE_CENTRO_HIPICO";
const ROLE_OPERADOR = "ROLE_OPERADOR";
/**
* @var string Name of this type
*/
protected $name = 'AdminRoleType';
/**
* @var array Readable choices
* @static
*/
protected static $choices = [
self::ROLE_ADMIN => 'Administrador',
self::ROLE_GERENTE => 'Gerente',
self::ROLE_FISCAL => 'Fiscal',
self::ROLE_CENTRO_HIPICO => 'Centro Hípico',
self::ROLE_OPERADOR => 'Operadora'
];
}
Then in my form type I have a field like this:
....
->add('roleType', 'choice', array(
'choices' => AdminRoleType::getChoices(),
'required' => true,
'label' => "User Type",
'trim' => true
))
....
In one view I need to render just ROLE_ADMIN
and ROLE_OPERADOR
but in another view I need to render all of them, how I do that?
I found the solution for my issue, just need to add as many methods as I want to return what I need, see the example below:
class RoleType extends AbstractEnumType
{
const ROLE_ADMIN = "ROLE_ADMIN";
const ROLE_GERENTE = "ROLE_GERENTE";
const ROLE_FISCAL = "ROLE_FISCAL";
const ROLE_CENTRO_HIPICO = "ROLE_CENTRO_HIPICO";
const ROLE_OPERADOR = "ROLE_OPERADOR";
/**
* @var string Name of this type
*/
protected $name = 'RoleType';
/**
* @var array Readable choices
* @static
*/
protected static $choices = [
self::ROLE_ADMIN => 'Administrador',
self::ROLE_GERENTE => 'Gerente',
self::ROLE_FISCAL => 'Fiscal',
self::ROLE_CENTRO_HIPICO => 'Centro Hípico',
self::ROLE_OPERADOR => 'Operadora'
];
public static function getFrontChoices()
{
return [
self::ROLE_CENTRO_HIPICO => 'Centro Hípico',
self::ROLE_OPERADOR => 'Operadora'
];
}
}
Then at form if I want just to show ROLE_CENTRO_HIPICO
and ROLE_OPERADOR
instead of call getChoices()
I call getFrontChoices()
:
....
->add('roleType', 'choice', array(
'choices' => AdminRoleType::getFrontChoices(),
'required' => true,
'label' => "User Type",
'trim' => true
))
....
That's all