zendframework / zend-form

Form component from Zend Framework
BSD 3-Clause "New" or "Revised" License
69 stars 87 forks source link

Having no defined Input elements in Fieldset throws fatal on data binding #215

Closed rkeet closed 5 years ago

rkeet commented 6 years ago

I have a Form with a Fieldset, an InputFilter for both. The Fieldset is a baseFieldset.

The Fieldset contains no mapped Input elements, it just has a submit button.

During execution of binding data on the fieldset it comes across line 365 of the Form class.

        // If there is a base fieldset, only hydrate beginning from the base fieldset
        if ($this->baseFieldset !== null) {
            $data = $data[$this->baseFieldset->getName()];
            $this->object = $this->baseFieldset->bindValues($data, $validationGroup[$this->baseFieldset->getName()]); // <-- That's line 365
        } else {
            $this->object = parent::bindValues($data, $validationGroup);
        }

If you have no mapped Elements, the data is empty. It's just a submit action. That means that when binding line 364 sets $data to be null.

Function declaration of Fieldset->bindValues(...):

public function bindValues(array $values = [], array $validationGroup = null)

If the first argument is null (because line 364), then this causes a fatal error.

Updating line 364 to the following fixes the problem: first checks if the key exists, if yes continues as it did, else sets $data as an empty array.

    $data = isset($data[$this->baseFieldset->getName()]) ? $data[$this->baseFieldset->getName()] : [];
froschdesign commented 6 years ago

@rkeet

first checks if the key exists

Then use the right function for that.

rkeet commented 6 years ago

+1

Usually "just used" isset, now a quick comparison between the 2 does make array_key_exists come out on top.

Wondering if just a simpler isset($data) ? $data : [] would actually suffice for the scenario. Thinking it would. Definitely wrong.

btw, working on a quick unit test to cover scenario.