为FOSUserBundle注册页面创建角色选项列表

时间:2014-11-17 16:39:19

标签: php symfony fosuserbundle

我正在尝试实施涵盖here主题的答案,但我遇到了问题!

加载注册表单时出现以下错误

Notice: Undefined index: roles

参考

in src/Ampisoft/Bundle/etrackBundle/Form/Type/RegistrationFormType.php at line 21

RegistrationFormType :: RegisterAction

我已经在这个片段的第二行测试了$ roles的输出,并且我有一个完整的数组。

//.......
$roles = $this->container->getParameter('security.role_hierarchy.roles');
    var_dump($roles);
    return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:register.html.'.$this->getEngine(), array(
        'form' => $form->createView(
            array('roles' => $roles,
            )),
    ));
// .......

问题似乎是我将$roles数组传递(或从中检索)到Type类的方式。

RegistrationFormType

class RegistrationFormType extends AbstractType
{
    protected $roles;

    public function __construct($options = array())
    {
        $this->roles = $options['roles']; // <--------------- THIS IS line 21
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // custom form fields
        $builder->add('firstname');
        $builder->add('lastname');
        $builder->add('roles', 'choice', array(
            'required' => true,
            'multiple' => true,
            'choices' => $this->refactorRoles($this->roles)
        ));
    }

    public function getDefaultOptions()
    {
        return array(
            'roles' => null
        );
    }

    private function refactorRoles($originRoles)
    {
        $roles = array();
        $rolesAdded = array();

        // Add herited roles
        foreach ($originRoles as $roleParent => $rolesHerit) {
            $tmpRoles = array_values($rolesHerit);
            $rolesAdded = array_merge($rolesAdded, $tmpRoles);
            $roles[$roleParent] = array_combine($tmpRoles, $tmpRoles);
        }
        // Add missing superparent roles
        $rolesParent = array_keys($originRoles);
        foreach ($rolesParent as $roleParent) {
            if (!in_array($roleParent, $rolesAdded)) {
                $roles['-----'][$roleParent] = $roleParent;
            }
        }

        return $roles;
    }

    public function getParent()
    {
        return 'fos_user_registration';
    }

    public function getName()
    {
        return 'etrack_user_registration';
    }

}

我尝试过将security.hierarchy.roles数组的内容传递给Type类的各种方法但无济于事。

0 个答案:

没有答案