Problems using "belongsTo" on an element within a "subform"

2 messages Options
Embed this post
Permalink
wiseguydigital

Problems using "belongsTo" on an element within a "subform"

Reply Threaded More More options
Print post
Permalink
Hi all,

I'm trying to build a Zend_Form which is built using quite a few subforms (ZendXjQuery forms). In only one of these subforms I have some elements which I would like to have added to an array, and therefore I am using belongsTo for those elements.

The HTML rendered is perfect for the form and displays exactly I would imagine with my elements being rendered like <input type="text" name="my_array-input_name" id="my_array[input_name]" value="0" />. So far so good.

The problem comes that when I use $form->isValid() in my controller, this seems to have issues with my arrayed elements. All validation messages are perfect, all the other form elements are fine...

BUT, say for instance I had inputted "999" as my value in one of the array fields, when I post the form, if validation is unsuccessful, then although the other elements in my form remember their posted values, the arrayed elements all get set to 0.

If I post the form with no validation problems, and use Zend_Debug::dump($this->_request->getPost()) in my controller, the array looks the way that I would imagine?! So my guess is that isValid() does not like the way I have built the form? And example of a successful posted array is something like:

array(47) {
  ["submit"] => string(4) "Save"
  ["request_token"] => string(32) "0b17c812728b415bb51fe1b65f548725"
  ["first_name"] => string(5) "foo"
  ["last_name"] => string(5) "bar"
  ["my_arrayed_element"] => array(10) {
    ["example_one"] => string(5) "baz"

...
}

This is the array format that I wish to receive the posted variables in, it's just getting it to stop forgetting the arrayed elements when validation is not successful.

### Small update:
I'm not sure if this'll help but I just stuck some code into the isValid() method of Zend/Form/Element.php and the results suggest that there is no value for the elements using belongsTo() (I know this is not the ideal way of testing but it is quick).

echo '<p>NAME: '.$this->getName();
echo '<br />VALUE: '.$value;
echo '<br />BELONGS TO: ' . $this->getBelongsTo();

All normal fields are fine, but the arrayed fields always seem to default to 0 even if I enter a value such as 25:

NAME: baz
VALUE: 0
BELONGS TO: my_arrayed_element
wiseguydigital

Re: Problems using "belongsTo" on an element within a "subform"

Reply Threaded More More options
Print post
Permalink
I think I may have found the answer, although again I could be wrong. I think it needed some code in the main Zend/Form.php file within the isValid() method. I will try to report my findings to ZF team through the Issue Tracker, but basically I added a new piece of code to find nested arrays. This is prefect for my site and will help me continue, but the actual code would probably need more work.

Within the isValid() method I have replaced:

    foreach ($this->getElements() as $key => $element) {
            $element->setTranslator($translator);
            if (!isset($data[$key])) {
                $valid = $element->isValid(null, $data) && $valid;
            } else {
                $valid = $element->isValid($data[$key], $data) && $valid;
            }
        }

with:

    foreach ($this->getElements() as $key => $element) {
            $element->setTranslator($translator);

            if (null != $element->getBelongsTo()) {
                $parent_array = $data[$element->getBelongsTo()];
                $key = $parent_array[$key];
                $valid = $element->isValid($key, $data) && $valid;
            } else {

                if (!isset($data[$key])) {
                    $valid = $element->isValid(null, $data) && $valid;
                } else {
                  $valid = $element->isValid($data[$key], $data) && $valid;
                }
            }
        }