You should inject the dependencies of the validator into the validator. You could do that through the options array when you assign the validator to a form field. I wrote up some sample code to demonstrate what I mean:
Register\Validator\MyValidation:
<?php
namespace Application\Validator;
use Zend\Validator\AbstractValidator;
class MyValidation extends AbstractValidator
{
protected $tableGateway;
public function __construct($options = null)
{
parent::__constructor($options);
if ($options && is_array($options) && array_key_exists('tableGateway', $options))
{
$this->tableGateway = $options['tableGateway'];
}
}
public function isValid($value)
{
// ...
}
}
As for the form you can either implement the ServiceLocatorAwareInterface, so it gets automatically injected with the service locator, or else inject specific dependencies into the form using a factory for the form.
Here's how you would do it using the ServiceLocatorAwareInterface:
Register\Form\MyForm:
<?php
namespace Register\Form;
use Zend\Form\Form;
use Zend\InputFilter\InputFilterProviderInterface;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
class MyForm extends Form implements InputFilterProviderInterface, ServiceLocatorAwareInterface
{
protected $servicelocator;
public function __construct()
{
$this->add(array(
'name' => 'myfield',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Field 1'
),
)
);
}
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
$this->servicelocator = $serviceLocator;
}
public function getServiceLocator()
{
return $this->servicelocator;
}
public function getInputFilterSpecification()
{
return array(
'myfield' => array(
'required' => true,
'filters' => array(),
'validators' => array(
array(
'name' => 'Application\Validator\MyValidator',
'options' => array(
'tableGateway' => $this->getServiceLocator()->get('Application\Model\RegisterTable'),
),
),
),
),
);
}
}
Also it's not clear to me why you are instantiating a Controller in your validator class. You really shouldn't do that.