ZF-7793: An element with the same name as its subform handles the input value incorrect


Take a look at the following code. It appears that elements that have the same name as the subform that is containing them, don't handle the input value correctly. In fact, the input value always seems to get truncated to the first char of the input string.

$form = new Zend_Form();

// Create a subform
$subform = new Zend_Form_SubForm();

// Add an element  with a validator to that subform
$font = new Zend_Form_Element_Text('font');
$font->addValidator( new Zend_Validate_StringLength(3));

// Now add the subform to the central form and give it the same name as the previous element
$form->addSubForm( $subform, 'font' );

// Provide some valid input
$form->isValid( array(
    'font' => array(
        'font' => 'a valid input string' ) ) );

// Output equals stringLengthTooShort
echo current($subform->font->getErrors());

// Output equals: t
// in fact, all input will be truncated to a lenght of 1 char
echo $subform->font->getValue();



Here's more code to confirm...

$subFormName = 'test';

$testElementNames = array(
    'pass' => 'anything',
    'fail' => $subFormName,

foreach ($testElementNames as $elementName) {
    $form = new Zend_Form;
    $subForm = new Zend_Form_SubForm;
    $subForm->addElement('text', $elementName);
    $subForm->addElement('text', 'otherField');
    $form->addSubForm($subForm, $subFormName);
        $subFormName => array(
            $elementName => 'Test content.',
            'otherField' => 'More test content.',


  'test' => 
      'anything' => string 'Test content.' (length=13)
      'otherField' => string 'More test content.' (length=18)

  'test' => 
      'test' => string 'T' (length=1)
      'otherField' => string 'T' (length=1)

Note that the values for all elements in the subform are truncated to the value of the first letter of the rouge element.

It seems that the values returned are only mangled after calling "isValid" on the form. (For instance changing "isValid" in the code above to "setDefaults" results in the correct return values from "getValues".

Fixed in [ZF-9348]

Reopened because suggested fix is not reviewed and committed yet.

Matthew Weier O'Phinney resolved [ZF-9348] Patch applied to trunk and 1.10 release branch.