I'm trying to add validator RecordExists to my form but I get error 'no db adapter present'. How can I set db adapter to this validator? I use examples from skeleton application and I'm trying to do something like this (yes, I know that $dbAdapter is undefined :) I'm searching solution how to change this variable to db adapter resource ):
namespace Album\Model;
use Zend\InputFilter\Factory as InputFactory; // <-- Add this import
use Zend\InputFilter\InputFilter; // <-- Add this import
use Zend\InputFilter\InputFilterAwareInterface; // <-- Add this import
use Zend\InputFilter\InputFilterInterface; // <-- Add this import
class Album implements InputFilterAwareInterface
{
public $id;
public $artist;
public $title;
protected $inputFilter; // <-- Add this variable
public function exchangeArray($data)
{
$this->id = (isset($data['id'])) ? $data['id'] : null;
$this->artist = (isset($data['artist'])) ? $data['artist'] : null;
$this->title = (isset($data['title'])) ? $data['title'] : null;
}
// Add content to this method:
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Not used");
}
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'),
),
'validators' => array(
array(
'name' => 'Db\RecordExists',
'options' => array(
'table' => 'album',
'field' => 'title',
'adapter' => $dbAdapter
),
),
),
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}