Created
October 18, 2013 21:30
-
-
Save austinsmorris/7048564 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, | |
], | |
], | |
]; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function getMessages($elementName = null) | |
{ | |
// TODO - This function exists as a work around for https://github.com/zendframework/zf2/issues/5265 | |
$messages = parent::getMessages($elementName); | |
if (null === $elementName) { | |
foreach ($this->messages as $error => $message) { | |
$messages[$error] = $message; | |
} | |
} | |
return $messages; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function setMessages($messages) | |
{ | |
// TODO - This function exists as a work around for https://github.com/zendframework/zf2/issues/5265 | |
foreach ($messages as $key => $messageSet) { | |
if (!$this->has($key)) { | |
$this->messages[$key] = $messageSet; | |
} | |
} | |
parent::setMessages($messages); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment