Issues

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

Description

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.


<?php
$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));
$subform->addElement($font);

// 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();

?>

Comments

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);
    $form->isValid(array(
        $subFormName => array(
            $elementName => 'Test content.',
            'otherField' => 'More test content.',
        )
    ));
    var_dump($form->getValues());
}

Output:


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

array
  'test' => 
    array
      '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.