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;
}
}
For doing the validation for "Username already exists or not", Do the following simple way of Service Manager config settings like:
// config/autoload/global.php
and this in your controller action to set the static db adapter first (if you did not use the adapter before)
and add the following array into getInputFilter():
I hope this helps to everyone. Cheers!!!
You can create 'Album/Model/Album' factory and inject dbAdapter to Album model.
Now we have to implement setDbAdapter and getDbAdapter methods:
You can get dbAdapter in your input filter by calling
$this->getDbAdapter();
Remember to get Album model in controller by ServiceLocator, otherwise dbAdapter won't be avaiable in your model.
@kierzniak provided a good way to inject DbAdapter, My solution is almost the same, not only inject DbAdapter but the full ServiceLocator, By injecting ServiceLocator, we could also get system configs, EventManagers, I think it is more flexible. Here is the solution:
Step 1: Make Album\Model implements ServiceLocatorAwareInterface, so that the service locator will be able to injected into Album\Model.
Step2, Register Album\Model as a service in config or Module.php
Step3, call AlbumModel by service but not a class:
Step4, now you could use DbAdapter in AlbumModel:
By above steps, you could share any of services in anywhere, hope this could help you.