ZF2 Error trying to use ZfcUser\Service\User::register method to register User from another controller

StackOverflow https://stackoverflow.com/questions/23197449

Question

I want to register a user using ZfcUser and add a model to the database within one form. So I extended the registration form and validate the data by giving the postdata to different forms like this:

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\Form;
use Zend\View\Model\ViewModel;

use Application\Model\Author;
use Application\Form\AuthorForm;
use Application\Form\Author2Form;

use ZfcUser\Service\User as UserService;

class AdminController extends AbstractActionController {
    protected $authorTable;
    protected $registerForm;
    protected $userService;

    public function authorAction() {
        $author = new Author();
        $form = $this->getRegisterForm();
        $formRegister = $this->getServiceLocator()->get('zfcuser_register_form');

        $formAuthor = new Author2Form();
        $request = $this->getRequest();
        $service = $this->getUserService();

        if ($request->isPost()) {
            $post = $request->getPost();
            $post['email'] = $post['mail'];
            $request->setPost($post);

            $formAuthor->setValidationGroup(array('name', 'last_name', 'mail', 'group_name', 'description'));
            $formRegister->setValidationGroup(array('email', 'password', 'passwordVerify'));
            $form->setData($request->getPost());
            $formAuthor->setData($request->getPost());
            $formRegister->setData($request->getPost());

            if ($formAuthor->isValid() && $formRegister->isValid()) {
                $service->register($formRegister->getData());
                $author->exchangeArray($formAuthor->getData());
                $this->getAuthorTable()->saveAuthor($author);

                return $this->redirect()->refresh();
            }
        }

        return new ViewModel(array(
            'authorList' => $this->getAuthorTable()->fetchAll(),
            'authorForm' => $form,
        ));
    }

    public function getRegisterForm() {
        if (!$this->registerForm) {
            $this->setRegisterForm($this->getServiceLocator()->get('Application\Form\AuthorForm'));
        }
        return $this->registerForm;
    }

    public function setRegisterForm(AuthorForm $registerForm) {
        $this->registerForm = $registerForm;
    }

    public function getUserService() {
        if (!$this->userService) {
            $this->userService = new UserService();
        }
        return $this->userService;
    }

    public function setUserService(UserService $userService) {
        $this->userService = $userService;
        return $this;
    }

}

In the Module.php I added the following code under factories:

                'Application\Form\AuthorForm'     => function($sm) {
                    $options = $sm->get('zfcuser_module_options');
                    $form = new AuthorForm('authorForm', $options);
                    return $form;
                },

I am able to validate both forms and then the following error appears:

Fatal error: Call to a member function get() on a non-object in /var/www/*/vendor/zf-commons/zfc-user/src/ZfcUser/Service/User.php on line 241

That would be the following method:

    public function getOptions() {
    if (!$this->options instanceof UserServiceOptionsInterface) {
        $this->setOptions($this->getServiceManager()->get('zfcuser_module_options'));
    }
    return $this->options;
}

I tried already so many things to accomplish that and I just dont know what to try anymore..

Can anybody please help me?

Was it helpful?

Solution

Hey put this in your Controller:

$service->setServiceManager($this->getServiceLocator());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top