Created
October 18, 2013 21:19
-
-
Save austinsmorris/7048425 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace My\Form; | |
use Zend\Form\Element; | |
use Zend\Form\Fieldset; | |
use Zend\InputFilter\InputFilterProviderInterface; | |
use Zend\Validator\InArray; | |
/** | |
* Class FooFieldset | |
* @package My\Form | |
*/ | |
class FooFieldset extends Fieldset implements InputFilterProviderInterface | |
{ | |
/** | |
* Constructor. | |
* | |
* @param string|null $name | |
* @param array $options | |
*/ | |
public function __construct($name = null, $options = []) | |
{ | |
parent::__construct($name, $options); | |
$select = (new Element\Select('code')) | |
->setAttributes(['options' => ['' => '', 'a' => 'Option 1', 'b' => 'Option 2', 'c' => 'Option 3']]); | |
$this->add($select); | |
} | |
/** | |
* Get the input filter specification. | |
* | |
* @return array | |
*/ | |
public function getInputFilterSpecification() | |
{ | |
$validCodes = ['' => '', 'a' => 'Option 1', 'b' => 'Option 2', 'c' => 'Option 3']; | |
$inArrayValidator = (new InArray())->setHaystack($validCodes)->setStrict(InArray::COMPARE_STRICT); | |
return [ | |
'code' => [ | |
'required' => true, | |
'filters' => [ | |
['name' => 'Zend\Filter\StringTrim'], | |
['name' => 'Zend\I18n\Filter\Alpha'], | |
['name' => 'Zend\Filter\StringToLower'], | |
], | |
'validators' => [ | |
['name' => 'Zend\Validator\NotEmpty'], | |
$inArrayValidator, | |
], | |
], | |
]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace My\Form; | |
use My\Repository\MyRepositoryInterface; | |
use Zend\Form\Element; | |
use Zend\Form\Form; | |
use Zend\InputFilter\InputFilterProviderInterface; | |
/** | |
* Class FooForm | |
* @package My\Form | |
*/ | |
class FooForm extends Form implements InputFilterProviderInterface | |
{ | |
/** | |
* @var MyRepositoryInterface | |
*/ | |
protected $myRepository; | |
/** | |
* Constructor. | |
* @param FooFieldset $fooFieldset | |
* @param MyRepositoryInterface $myRepository | |
* @param string|null $name | |
* @param array $options | |
*/ | |
public function __construct( | |
FooFieldset $fooFieldset, | |
MyRepositoryInterface $myRepository, | |
$name = null, | |
$options = [] | |
) { | |
$this->myRepository = $myRepository; | |
parent::__construct($name, $options); | |
$fooFieldset->setName('foo')->setOptions(array('use_as_base_fieldset' => true)); | |
$csrf = new Element\Csrf('csrf'); | |
$submit = (new Element\Submit('submit'))->setValue('Save Customer Role'); | |
$this->add($fooFieldset); | |
$this->add($csrf); | |
$this->add($submit); | |
} | |
/** | |
* Get the input filter specification. | |
* | |
* @return array | |
*/ | |
public function getInputFilterSpecification() | |
{ | |
return [ | |
'foo' => [ | |
'validators' => [ | |
[ | |
'name' => 'My\Validator\NoObjectExists', | |
'options' => [ | |
'object' => $this->object, | |
'repository' => $this->myRepository, | |
], | |
], | |
], | |
], | |
]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace My\Validator; | |
use BadMethodCallException; | |
use InvalidArgumentException; | |
use My\Repository\MyRepositoryInterface; | |
use Zend\Validator\AbstractValidator; | |
/** | |
* Class NoObjectExists | |
* @package My\Validator | |
*/ | |
class NoObjectExists extends AbstractValidator | |
{ | |
/** | |
* Error constants | |
*/ | |
const OBJECT_EXISTS = 'objectExists'; | |
/** | |
* Validation error messages. | |
* | |
* @var array | |
*/ | |
protected $messageTemplates = [ | |
self::OBJECT_EXISTS => "A record already exists for this selection", | |
]; | |
protected $options = [ | |
'object' => null, | |
'repository' => null, | |
]; | |
/** | |
* {@inheritDoc} | |
*/ | |
public function __construct($options = null) | |
{ | |
if (!isset($options['object']) || !is_object($options['object'])) { | |
throw new InvalidArgumentException("The 'object' option must be set."); | |
} | |
if (!isset($options['repository']) || !($options['repository'] instanceof MyRepositoryInterface)) { | |
throw new InvalidArgumentException( | |
"The 'repository' option must be an instance of " | |
. "My\\Repository\\PersistenceRepositoryInterface." | |
); | |
} | |
parent::__construct($options); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function isValid($value) | |
{ | |
$this->setValue($value); | |
if (!is_array($value)) { | |
throw new BadMethodCallException('Cannot validate unnamed inputs. $value must be an array'); | |
} | |
$repository = $this->getOption('repository'); | |
$object = clone $this->getOption('object'); | |
if ($repository->getObject($object)) { | |
$this->error(self::OBJECT_EXISTS); | |
return false; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment