0
votes

I've got a simple user registration form where a user can choose their own user type. The user type maps to a role. This is part of a zf2 application using the doctrine2 module.

The relevant part of the init() method of my user fieldset looks like this: public function init() { // ... other field definitions ...

    $roleRadio = new ObjectRadio('role');
    $roleRadio->setLabel('What type of user are you?')
              ->setOptions(
                    array(
                        'object_manager' => $this->objectManager,
                        'target_class'   => 'MyUser\Entity\Role',
                        'property'       => 'roleId',
                        'is_method'      => true,
                        'find_method'    => array(
                            'name'   => 'findBy',
                            'params' => array(
                                'criteria' => array('userselectable' => true),
                                'orderBy'  => array('displayorder' => 'ASC'),
                            ),
                        ),
                    )
    );

    $this->add($roleRadio);

    // ... more stuff ...
}

I'm using Doctrine's ObjectRadio class for this element to automatically populate the value options. Is there any way to set the default selected value? I know I can just do something like this:

$form->get('user')->get('role')->setValue(3);

But I don't want to hard code this and I also don't want to put that kind of logic in my controller.

Any suggestions?

1

1 Answers

1
votes

I don't know what do you mean by "I don't want to hard code this", but you can do it as you said in your controller, or you can do it in the form definition by setting attributes as the following:

$roleRadio->setAttributes(array('value' => 3));