I created a Validator for one of my form fields. To do that, I need the ServiceLocator so I would like to use a factory ...
Edit :
Here is my factory :
namespace Maintenance\Factory\Validator;
/* Zend */
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
/* Controller */
use Maintenance\Validator\Echeancedebut;
class EcheancedebutFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
$realServiceLocator = $serviceLocator->getServiceLocator();
$maiContratService = $realServiceLocator->get(
'Maintenance\Service\Model\FMaiContratService'
);
return new Echeancedebut($maiContratService);
}
}
My Validator :
class Echeancedebut extends AbstractValidator
{
const ERROR_DATEDEB = 'ERROR_DATEDEB';
protected $maiContratService;
protected $messageTemplates = array(
self::ERROR_DATEDEB => "Saisie inférieure à la date de début du contrat"
);
public function __construct($maiContratService) {
$this->maiContratService = $maiContratService;
}
public function isValid($value){
$this->setValue($value);
if (!$this->validatedate($value)) {
$this->error(self::ERROR_DATEDEB);
return false;
}
return true;
}
private function validatedate($date) {
return false;
}
}
My InputFilter:
public function getInputFilter()
{
if (! $this->inputFilter) {
$inputFilter = new InputFilter();
$this->inputFilter = $inputFilter;
}
$inputFilter->add(
array(
'name' => 'dateDeb',
'required' => true,
'allow_empty' => false,
'validators' => array(
array(
'name' => 'Date',
'locale' => 'FR_fr',
'options' => array(
'format' => 'd/m/Y',
),
),
array(
'name' => 'Maintenance\Validator\Echeancedebut',
'options' => array(
'contratId' => $this->iMaiContratId,
)
);
return $this->inputFilter;
}
The problem is that it doesn't retrieve my error message, why ?
The name of your factory and the name you registered in your config are not corresponding:
Maintenance\Factory\Validator\EcheancedebutFactory
(inside your config)
Maintenance\Factory\Validator\EcheancedebFactory
(the full class name)
Echeancedebut
vs Echeancedeb
.
Change that and I think it should work.