Richard Parnaby-King
Richard Parnaby-King

Reputation: 14891

How to exclude an element in a fieldset when calling getData on a form?

I have a fieldset with some elements, and a button. When I submit the form I am getting an empty value for the button. How do I tell ZF2 to not include the button in the return data?

//In fieldset

//...other elements

$this->add([
    'name' => 'add-question',
    'type' => 'button',
    'attributes' => [
        'onclick' => 'return add_question(this)',
        'value' => 'Add Add-On',
        'class' => 'add-element text-primary',
    ],
    'options' => [
        'label' => '',
        'label_options' => [
            'disable_html_escape' => true,
        ],
    ],
]);

//In controller
if($this->request->isPost()) {
    $form->setData($this->request->getPost());
    if($form->isValid()) {
        var_dump($form->getData());
        /*
        array (size=12)
            //... other elements
           'add-ons' => array (size=2)
               //... other elements
               'add-question' => null
        */

I am aware on the from I can add a validation group, but I can't do that in a fieldset. Also, the form is just an empty form with a submit button in it for re-use - all my form elements are in fieldsets.

Upvotes: 0

Views: 189

Answers (1)

Greco Jonathan
Greco Jonathan

Reputation: 2544

I guess your Fieldsets extends \Zend\Form\Fieldset ?

If yes : You can't setValidationGroup in you Fieldsets as they are now because they're not implements \Zend\Form\Form.

As long as \Zend\Form\Form extends \Zend\Form\Fieldset, you should be able to use the setValidationGroup like you want to for all of your forms by using the extends \Form, without breaking anything.

Upvotes: 0

Related Questions