Skip to content

[zend-form] reject array values for single value elements - #243

Open
falkenhawk wants to merge 2 commits into
masterfrom
fix-form-element-array-value
Open

falkenhawk wants to merge 2 commits into
masterfrom
fix-form-element-array-value

Conversation

@falkenhawk

@falkenhawk falkenhawk commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

A tampered request like name[]=x sends an array to a text field.
Zend_Form_Element::isValid() passes the array on to the filters and validators:

$element = new Zend_Form_Element_Text('name', array('required' => true, 'filters' => array('StringTrim')));
$element->isValid(array('x')); // true, with "Array to string conversion" warnings
$element->getValue();          // 'Array'
  • string filters like StringTrim turn the array into the text 'Array', with a warning. That passes NotEmpty and any validator accepting this text, so the form is valid and the app saves 'Array'.
  • filters and validators that do not convert the value, e.g. Zend_Filter_Callback with trim or custom ones, throw a TypeError on php 8
  • without filters, a form that failed validation shows value="Array" in the field, with a warning

Fix

A new protected $_rejectArrayValue flag on Zend_Form_Element, off by default.
It is on for the built-in elements holding a single value: Text, Textarea, Password, Hidden, Hash, Select and Radio.
Such an element, unless it is set up as an array element with setIsArray(true), now drops an array value and fails validation with the error invalidType, "Invalid type given. String expected".
The standard validators use the same message, so existing translations apply.
Custom messages set with setErrorMessages() are used instead, as for validators.

Backwards compatibility

  • Zend_Form_Element and Zend_Form_Element_Xhtml still accept arrays. Custom elements built from several inputs, which usually extend Zend_Form_Element_Xhtml and take an array in setValue(), keep working.
  • the check runs after setValue(), so a subclass of Text and the others that turns an array into a single value in setValue() keeps working too
  • array elements (setIsArray(true), MultiCheckbox, Multiselect), Captcha, File, Checkbox and the buttons are not affected
  • regular requests are not affected

Not covered

Calling populate() with an array and then rendering the form still shows value="Array" with a warning.
That data comes from the app itself, it is not validated.

Ported from a private repository, based on work by @marcing. There, every element that is not an array element rejects arrays, which breaks custom elements built from several inputs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant