我可以将ServiceLocator注入验证器吗?

时间:2015-09-09 13:56:27

标签: php zend-framework2 zend-form zend-inputfilter

我为其中一个表单字段创建了一个Validator。为此,我需要ServiceLocator,所以我想使用工厂......

修改:

这是我的工厂:

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);
    }
}

我的验证员:

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;
    }
}

编辑:

我的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;
}

问题是它没有检索我的错误信息,为什么?

0 个答案:

没有答案