-
-
Save acelaya/78970ed8f428edc8dda8 to your computer and use it in GitHub Desktop.
Form collection validation
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 Application\Form; | |
use Zend\Form\Form; | |
use Zend\InputFilter\InputFilterInterface; | |
use Zend\Form\Element\Collection; | |
use Application\Form\TranslationFieldset; | |
class TheForm extends Form | |
{ | |
const DESCRIPTIONS = "descriptions"; | |
const DESCRIPTION = "description"; | |
public function __construct(InputFilterInterface $inputFilter, $name = "TheForm") | |
{ | |
parent::__construct($name); | |
$this->initElements() | |
->setInputFilter($inputFilter); | |
} | |
protected function initElements() | |
{ | |
$descriptionsElement = new Collection(self::DESCRIPTIONS); | |
$descriptionsElement->setTargetElement(new TranslationFieldset(self::DESCRIPTION)); | |
$this->add($descriptionsElement); | |
return $this; | |
} | |
} |
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 Application\Form; | |
use Zend\InputFilter\InputFilter; | |
use Zend\InputFilter\Input; | |
use Zend\InputFilter\CollectionInputFilter; | |
use Zend\Filter\StringTrim; | |
use Zend\Filter\StripTags; | |
class TheInputFilter extends InputFilter | |
{ | |
public function __construct() | |
{ | |
$this->initElements(); | |
} | |
protected function initElements() | |
{ | |
$descriptionsInputFilter = new InputFilter(); | |
$messageInput = new Input(TranslationFieldset::MESSAGE); | |
$messageInput->getFilterChain()->attach(new StringTrim()) | |
->attach(new StripTags()); | |
$descriptionsInputFilter->add($messageInput); | |
$descriptionsContainerFilter = new CollectionInputFilter(); | |
$descriptionsContainerFilter->setInputFilter($descriptionsInputFilter) | |
->setIsRequired(false); | |
$this->add($descriptionsContainerFilter, TheForm::DESCRIPTIONS); | |
return $this; | |
} | |
} |
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 Application\Form; | |
use Zend\InputFilter\InputFilter; | |
use Zend\InputFilter\Input; | |
use Zend\InputFilter\CollectionInputFilter; | |
use Zend\Filter\StringTrim; | |
use Zend\Filter\StripTags; | |
class TheOtherInputFilter extends InputFilter | |
{ | |
public function __construct() | |
{ | |
$this->initElements(); | |
} | |
protected function initElements() | |
{ | |
$descriptionsInputFilter = new InputFilter(); | |
$messageInput = new Input(TranslationFieldset::MESSAGE); | |
$messageInput->getFilterChain()->attach(new StringTrim()) | |
->attach(new StripTags()); | |
$descriptionsInputFilter->add($messageInput); | |
$languageInput = new Input(TranslationFieldset::LANGUAGE); | |
$languageInput->getFilterChain()->attach(new StringTrim()) | |
->attach(new StripTags()); | |
$descriptionsInputFilter->add($languageInput); | |
$descriptionsContainerFilter = new CollectionInputFilter(); | |
$descriptionsContainerFilter->setInputFilter($descriptionsInputFilter) | |
->setIsRequired(false); | |
$this->add($descriptionsContainerFilter, TheForm::DESCRIPTIONS); | |
return $this; | |
} | |
} |
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 Application\Form; | |
use Zend\Form\Fieldset; | |
use Zend\Form\Element\Select; | |
use Zend\Form\Element\Text; | |
class TranslationFieldset extends Fieldset | |
{ | |
const LANGUAGE = "language"; | |
const MESSAGE = "message"; | |
public function __construct($name) | |
{ | |
parent::__construct($name); | |
$this->initElements(); | |
} | |
protected function initElements() | |
{ | |
$languageElement = new Select(self::LANGUAGE); | |
$languageElement->setValueOptions( | |
array('es' => 'es', 'en' => 'en', 'fr' => 'fr', 'de' => 'de') | |
); | |
$this->add($languageElement); | |
$messageElement = new Text(self::MESSAGE); | |
$this->add($messageElement); | |
return $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment