1
votes

Catchable fatal error: Argument 1 passed to Zend\Validator\Db\AbstractDb::setAdapter() must be an instance of Zend\Db\Adapter\Adapter, null given, called in /home2/mapasgua/vendor/zendframework/zendframework/library/Zend/Validator/AbstractValidator.php on line 142 and defined in /home2/mapasgua/vendor/zendframework/zendframework/library/Zend/Validator/Db/AbstractDb.php on line 168

Module.php

    <?php

namespace User;

use Zend\Mvc\MvcEvent;
use Zend\Mvc\ModuleRouteListener;

use User\Model\UsersTable;
use User\Model\Users;

class Module
{
    public function onBootstrap(MvcEvent $e)
    {
        ini_set('date.timezone', 'America/Sao_Paulo');

        $e->getApplication()->getServiceManager()->get('translator');
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);

        $e->getApplication()->getEventManager()->getSharedManager()->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($e) {
            $controller = $e->getTarget();
            $controllerClass = get_class($controller);
            $moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\\'));
            $config = $e->getApplication()->getServiceManager()->get('config');
            if (isset($config['module_layouts'][$moduleNamespace])) {
                $controller->layout($config['module_layouts'][$moduleNamespace]);
            }
        }, 100);
    }

    public function getAutoloaderConfig()
    {
        return array(
            // 'Zend\Loader\ClassMapAutoloader' => array(
            //     __DIR__ . '/autoload_classmap.php',
            // ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }

    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'User\Model\UsersTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table = new UsersTable($dbAdapter);
                    return $table;
                },
                'User\Model\Users' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $users = new Users();
                    $users->setDbAdapter($dbAdapter);
                    return $users;
                },
            ),
        );
    } 

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }
}

Users.php

    <?php

namespace User\Model;

use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilterInterface;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\Db\Adapter\Adapter;
use Zend\Validator\Db\AbstractDb;

class Users implements InputFilterAwareInterface
{
    public $id;
    public $name;
    public $username;
    public $password;

    protected $inputFilter;
    public $_dbAdapter;

    public function exchangeArray($data)
    {
        $this->id       = (isset($data['id'])) ? $data['id'] : null;
        $this->name     = (isset($data['name'])) ? $data['name'] : null;
        $this->username = (isset($data['username'])) ? $data['username'] : null;
        $this->password = (isset($data['password'])) ? $data['password'] : null;
    }

    public function getArrayCopy()
    {
        return get_object_vars($this);
    }

    public function setInputFilter(InputFilterInterface $inputFilter)
    {
        throw new \Exception("Not used");
    }

    public function setDbAdapter($dbAdapter) 
    {
        $this->_dbAdapter = $dbAdapter;
    }

    public function getDbAdapter() 
    {
        return $this->_dbAdapter;
    }

    public function getInputFilter()
    {
        if (!$this->inputFilter) {
            $inputFilter = new InputFilter();
            $factory = new InputFactory();

            $inputFilter->add($factory->createInput(array(
                'name' => 'id',
                'required' => true,
                'filters' => array(
                    array('name' => 'Int'),
                ),
            )));

            $inputFilter->add($factory->createInput(array(
                'name' => 'name',
                'required' => true,
                'filters' => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name' => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min' => 3,
                            'max' => 32,
                        ),
                    ),
                ),
            )));

            $inputFilter->add($factory->createInput(array(
                'name' => 'username',
                'required' => true,
                'filters' => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name' => 'EmailAddress',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min' => 3,
                            'max' => 32,
                            'message' => 'Endereço de e-mail invalido',
                        ),
                    ),
                    array(
                        'name'    => 'Db\NoRecordExists',
                        'options' => array(
                            'table' => 'users',
                            'field' => 'username',
                            'adapter' => $this->getDbAdapter(),
                            'exclude' => array(
                                'field' => 'id',
                                'value' => !is_null( $this->id ) && !empty( $this->id ) ? $this->id : 0,
                            ),
                        ),
                    ),
                ),
            )));

            $inputFilter->add($factory->createInput(array(
                'name' => 'password',
                'required' => true,
                'filters' => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name' => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min' => 3,
                            'max' => 32,
                        ),
                    ),
                ),
            )));

            $inputFilter->add($factory->createInput(array(
                'name' => 'retype-password',
                'required' => true,
                'filters' => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name' => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min' => 3,
                            'max' => 32,
                        ),
                    ),
                    array(
                        'name' => 'Identical',
                        'options' => array(
                            'token' => 'password', //I have tried $_POST['password'], but it doesnt work either
                        ),
                    ),
                ),
            )));

            $this->inputFilter = $inputFilter;
        }
        return $this->inputFilter;
    }
}

UserController.php

namespace User\Controller;

<?php
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as AuthAdapter;
use Zend\Authentication\Result as Result;
use Zend\Mail;
use User\Form\LoginForm;
use User\Form\RegisterForm;
use User\Form\RecuperarForm;
use User\Model\Users;

class UserController extends AbstractActionController
{
    protected $usersTable;

    public function configAction()
    {
        $auth = new AuthenticationService();
        $form = new RegisterForm();

        $identity = null;
        if ($auth->hasIdentity()) {
            $dados = $this->getUsersTable()->getUser($auth->getIdentity());
            $request = $this->getRequest();
            if ($request->isPost()) {
                $users = new Users();
                $post = $request->getPost();

                $form->setInputFilter($users->getInputFilter());
                $form->setData($post);
                    if ($form->isValid()) {
                        $users->exchangeArray($post);
                        $this->getUsersTable()->saveUsers($users);

                        $this->flashMessenger()->addMessage("Configurações atualizadas com sucesso.");
                        return $this->redirect()->toRoute('painel');
                    }
            } else {
                $post = array(
                    'id' => $dados->id,
                    'name' => $dados->name,
                    'username' => $dados->username,
                );
                $form->setData($post);
            }
        } else {
            return $this->redirect()->toRoute('login');
        }

        return array(
            'form' => $form,
        );
    }
1
Did you try with protected variable instead private?Remi Thomas
yes, return: Catchable fatal error: Argument 1 passed to Zend\Validator\Db\AbstractDb::setAdapter() must be an instance of Zend\Db\Adapter\Adapter, null given, called in /home/k13/braup/vendor/zendframework/zendframework/library/Zend/Validator/AbstractValidator.php on line 142 and defined in /home/k13/braup/vendor/zendframework/zendframework/library/Zend/Validator/Db/AbstractDb.php on line 168Jhon Mike
Is $dbAdapter in the factory definitely not null?Aydin Hassan
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');Jhon Mike

1 Answers

1
votes

Your problem is you have defined a factory for Users but you don't use it. The line $users = new Users(); directly instantiates the object without using your factory.

Change it to

$users = $this->getServiceLocator()->get('User\Model\Users');

As your object relies on Zend\Db\Adapter\Adapter you need your factory to inject this for you.