Skip to content

Instantly share code, notes, and snippets.

@austinsmorris
Created October 18, 2013 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save austinsmorris/7048564 to your computer and use it in GitHub Desktop.
Save austinsmorris/7048564 to your computer and use it in GitHub Desktop.
<?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