Skip to content

Instantly share code, notes, and snippets.

@asgrim
Last active December 18, 2015 23:59
Show Gist options
  • Save asgrim/5865719 to your computer and use it in GitHub Desktop.
Save asgrim/5865719 to your computer and use it in GitHub Desktop.
<?php
return array(
// this doesn't seem to do anything
'service_manager' => array(
'invokables' => array(
'Vendor\Validator\StartsWith' => 'Vendor\Validator\StartsWith',
),
'alias' => array(
'StartsWith' => 'Vendor\Validator\StartsWith',
),
),
// nor does this
'validators' => array(
'invokables' => array(
'Vendor\Validator\StartsWith' => 'Vendor\Validator\StartsWith',
),
'alias' => array(
'StartsWith' => 'Vendor\Validator\StartsWith',
),
),
);
<?php
namespace Vendor\Form;
use Zend\Form\Form;
use Zend\Form\Element;
use Zend\Form\Annotation\InputFilter;
use Zend\InputFilter\InputFilterProviderInterface;
class MyForm extends Form implements InputFilterProviderInterface
{
public function __construct(array $websites, $name = null)
{
parent::__construct($name);
// ... elements
}
public function getInputFilterSpecification()
{
return array(
'name' => array(
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StartsWith', // works with Vendor\Validator\StartsWith
'options' => array(
'pattern' => '/^[a-z]/',
),
),
),
),
);
}
}
Zend\ServiceManager\Exception\ServiceNotFoundException
File:
/home/tracked/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:496
Message:
Zend\Validator\ValidatorPluginManager::get was unable to fetch or create an instance for StartsWith
<?php
namespace Vendor\Validator;
use Zend\Validator\AbstractValidator;
use Traversable;
use Zend\Stdlib\ArrayUtils;
/**
* Validator to check that the input starts with the specified pattern
*/
class StartsWith extends AbstractValidator
{
const NO_MATCH = 'noMatch';
/**
* Digits filter used for validation
*
* @var \Zend\Filter\Digits
*/
protected static $filter = null;
/**
* Validation failure message template definitions
*
* @var array
*/
protected $messageTemplates = array(
self::NO_MATCH => "This name must begin with a characters from the pattern '%pattern%'",
);
/**
* @var array
*/
protected $messageVariables = array(
'pattern' => 'pattern'
);
/**
* The pattern to match against
*
* @var mixed
*/
protected $pattern;
/**
* Sets validator options
*
* @param array|Traversable $options
* @throws \InvalidArgumentException
*/
public function __construct($options = null)
{
if ($options instanceof Traversable)
{
$options = ArrayUtils::iteratorToArray($options);
}
if (!is_array($options))
{
$options = func_get_args();
$temp['pattern'] = array_shift($options);
$options = $temp;
}
if (!array_key_exists('pattern', $options) || is_null($options['pattern']))
{
throw new \InvalidArgumentException("Missing option 'pattern'");
}
$this->setPattern($options['pattern']);
parent::__construct($options);
}
/**
* Returns the pattern
*
* @return string pattern
*/
public function getPattern()
{
return $this->pattern;
}
/**
* Sets the pattern
*
* @param string $pattern
* @return StartsWith Provides a fluent interface
*/
public function setPattern($pattern)
{
$this->pattern = $pattern;
return $this;
}
/**
* Returns true if and only if $value only contains digit characters
*
* @param string $value
* @return bool
*/
public function isValid($value)
{
die("validator running!");
}
}
@asgrim
Copy link
Author

asgrim commented Jun 26, 2013

The Problem

I can't seem to get Zend\Form to load my validator from the service manager. My ultimate goal is my validator is loaded from the service manager because I need to inject a dependency.

What is going wrong? What have I missed about this? What do I not understand? :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment