Hi all, I got a weird situation with Zend Form, Collection with Fieldset. I don't know how to explain this in detail, so I bring the code example here.
I get an unexpected collection value from $form->getData() when
getInputFilter() is called before setData(). Here is the code (i use
ZendSkeleton in this example):
<?php
// Application/Controller/FormController.php
namespace Application\Controller;
class FormController extends \Zend\Mvc\Controller\AbstractActionController
{
public function indexAction()
{
$data = array(
'username' => ' user1 ',
'groups' => array(
array(
'name' => 'group1',
),
array(
'name' => 'group2',
),
array(
'name' => 'group3',
),
),
);
// case 1
$form = $this->createForm();
$form->getInputFilter()->get('username')->getFilterChain()->attachByName('stringtrim');
$form->setData($data);
$valid = $form->isValid();
$formData = $form->getData();
var_dump(count($formData['groups'])); // int(0), I expect this to
be int(3)
// case 2
$form = $this->createForm();
$form->setData($data);
$form->getInputFilter()->get('username')->getFilterChain()->attachByName('stringtrim');
$valid = $form->isValid();
$formData = $form->getData();
var_dump(count($formData['groups'])); // int(3)
return array('form' => $form);
}
protected function createForm()
{
$form = new \Zend\Form\Form;
$form->add(array(
'type' => 'text',
'name' => 'username',
'options' => array(
'label' => 'Username',
),
));
$form->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'groups',
'options' => array(
'label' => 'Please choose groups',
'count' => 0,
'should_create_template' => true,
'allow_add' => true,
'target_element' => array(
'type' => 'Application\Form\GroupFieldset',
),
),
));
return $form;
}
}
<?php
// Application/Form/GroupFieldset.php
namespace Application\Form;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;
class GroupFieldset extends Fieldset implements InputFilterProviderInterface
{
public function __construct()
{
parent::__construct('group');
$this->setLabel('Group');
$this->add(array(
'name' => 'name',
'options' => array(
'label' => 'Group name',
),
'attributes' => array(
'required' => 'required',
),
));
}
public function getInputFilterSpecification()
{
return array();
}
}
Is it my wrong? Would somebody explain to me. Thanks in advance.
--
Warmest regards,
Andy L.
<<attachment: FormController.php>>
<<attachment: GroupFieldset.php>>
-- List: [email protected] Info: http://framework.zend.com/archives Unsubscribe: [email protected]
