Hi,

I am using the FormElementManager to handle my forms. My forms are set
up in the init() method which is called by the FormElementManager on
instantiation. So far so good.

I have some forms which need additional setup, so I created factories
for them and configured these factories for the FormElementManager.
Within these factories the forms are created with a simple "new"
statement. Again, so far so good.

But now I have some of these factories:

------------------------------------------------------------------------
class ProfileFormFactory implements FactoryInterface
{
    public function createService(
        ServiceLocatorInterface $serviceLocator
    ) {
        $userEntity    = new UserEntity();
        $genderOptions = $userEntity->getData()->getProfile()
                                    ->getGenderOptions();

        $form = new ProfileForm();
        $form->get('data')
             ->get('profile')
             ->get('gender')
             ->setValueOptions($genderOptions);

        return $form;
    }
}
------------------------------------------------------------------------

After form creation I want to dynamically set some select options. But
this does not work yet, because the init() method is not called (which
should be done by the FormElementManager. When I add the init() call
after the form instantiation the form factory is not passed
automatically. So I would need something like this:

------------------------------------------------------------------------
        $form = new ProfileForm();
        $serviceLocator->injectFactory($form);
        $form->init();
        $form->get('data')
             ->get('profile')
             ->get('gender')
             ->setValueOptions($genderOptions);
------------------------------------------------------------------------

Feels really awkward to do the stuff the FormElementManager is normally
caring about.

So, my idea is something like this:

------------------------------------------------------------------------
class ProfileFormFactory implements FactoryInterface
{
    public function createService(
        ServiceLocatorInterface $serviceLocator
    ) {
        $userEntity    = new UserEntity();
        $genderOptions = $userEntity->getData()->getProfile()
                                    ->getGenderOptions();

        $form = new ProfileForm($genderOptions);
        return $form;
    }
}
------------------------------------------------------------------------

The constructor saves the options for later usage in a class property
and within the init() method I access the class property to set the options.

What do others think? Is this good or bad practice? How would you solve
these issues?

Thanks and regards,

Ralf

-- 
List: [email protected]
Info: http://framework.zend.com/archives
Unsubscribe: [email protected]


Reply via email to