i have problems validating a Form with a element of type Collection,
First, I create a "Collection" type element and then I add several text type elements.
the form is rendered correctly, The problem is that the Form is always valid.
How i can validate a collection type element?
Form class:
class TestForm extends Form {
private $inputFilter;
public function __construct($name = null) {
parent::__construct($name);
$this->add(array(
'name' => 'submit',
'type' => 'Zend\Form\Element\Submit',
'options' => array(
'label' => 'Submit',
),
'attributes' => array(
'class' => 'form-control',
'value' => 'submit'
),
));
$docs = array(
array('name' => "doc A"),
array('name' => "doc B")
);
// add collection of docs.
$collection = new \Zend\Form\Element\Collection();
$collection->setName('docs');
foreach ($docs as $key => $doc) {
$element = new \Zend\Form\Element\Text($key);
$element->setOptions(array(
'label' => $doc['name'],
));
$element->setAttributes(array(
'class' => 'form-control input-sm',
));
$collection->add($element);
}
$this->add($collection);
}
public function getInputFilter() {
$this->inputFilter = new InputFilter();
$this->inputFilter->add(array(
'name' => "docs",
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
));
return $this->inputFilter;
}
}
Controller class:
class IndexController extends AppController {
public function indexAction() {
$form = new \Application\Model\Form\TestForm();
$request = $this->getRequest();
if ($request->isPost()) {
$data = $this->params()->fromPost();
$form->setData($data);
$form->setInputFilter($form->getInputFilter());
if ($form->isValid()) {
pr("is valid");
} else {
pr($form->getMessages());
}
}
return new ViewModel(array(
'form' => $form
));
}
View class:
<?php
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formRow($form->get('docs'));
echo $this->formRow($form->get('submit'));
echo $this->form()->closeTag();
?>
Your Collection is always valid, because it contains fields. You can't do this that way.
You should considering to add Validators to DocA
and DocB
fields instead. This will work as follow to set correct input filters :
$form->getInputFilter()->get('docs')->get('DocA')->getValidatoChain()->attachByName('YourValidatorName');
for a custom validator.
OR :
$form->getInputFilter()->get('docs')->get('DocA')->setRequired(true);
$form->getInputFilter()->get('docs')->get('DocA')->setAllowEmpty(false);
And you can also add Zend validators to them.
$form->getInputFilter()->get('docs')->get('DocA')->getValidatorChain()->attach(new NotEmpty([with params look docs for that])
Careful if you don't use ServiceManager for retrieve validators you'll need to set the translator as options.
Don't forget to set your validationGroup correctly or don't specify one to use VALIDATE_ALL.
The same way as validators you can also add Filters as follow :
$form->getInputFilter()->get('docs')->get('DocA')->getFilterChain()->getFilters()->insert(new StripTags())->insert(new StringTrim())