1

I am trying to build multiple table form in zend framework 2. I have developed a single form by separating the field using HTML input array for example one field which belongs to table 'pages' looks like something

$this->add(array(
            'name' => 'pages[title]',
            'attributes' => array(
                'type' => 'text',
                'id' => 'title'
            ),
            'options' => array(
                'label' => 'Title',
            ),
        ));

But its filter is

$inputFilter->add($factory->createInput(array(
                        'name' => 'title',
                        'required' => true,
                        'filters' => array(
                            array('name' => 'StripTags'),
                            array('name' => 'StringTrim'),
                        ),
                        'validators' => array(
                            array(
                                'name' => 'NotEmpty',
                            ),
                        ),
            )));

The reason behind changing name is I am validating field step by step by assigning the values accordingly in my controller like following

$form->setData($this->request->getPost()->pages);

The problem is occurring while getting the array of messages. Form isValid() function is working fine but it returns empty array of messages in case of invalid input. How can I get the messages?


Upon digging deeper I have found out that filter is returning messages but when form is mapping error messages through the name of filter and element and as I am using different names it is unable to map... nay work around for this

And before you ask me to assign the same names to form elements and filters it will won't work because the form can't assign data to element with pages[title] because in request it won't receive any input with name pages[title] rather it will receive array of pages

1 Answer 1

1

I would take another approach, Keep your names simple, without adding in array name notation.

then you can validate different steps by using Validation Groups and Fieldsets.

http://framework.zend.com/manual/2.0/en/modules/zend.form.quick-start.html#validation-groups

If you can give more details about exactly what you're trying to achieve it will be easier to respond.

Sign up to request clarification or add additional context in comments.

3 Comments

the reason behind to set the names in array notation is to do the logical grouping of input on the basis of database tables to which they belong... you can say I am trying to implement the yii approach
@UmairAbid, I think you're missing the point Andrew's making, fieldsets actually do lead to forms constructed with array notation, the keys being the name of the fieldset, and it's elements being field names within, eg a fieldset named table1 having a field name of column1 would result in <input name="table1[column1]" ... /> in the html.
The Idea is to separate the form into fieldssets which can be bound to different objects, have a good read of the link ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.