1

I'm building a simple message system using Zend Form where users will be able to select multiple recipients. Each recipient may be a different user type and so I need to have both the user type and the ID to send to the servicelayer. The form will start off with one recipient selected (as an array element in the form), and I plan to use jQuery to append each subsequent recipient to said array. I wish to end up with this:

array(4) {
    ["Subject"] => string(4) "My message subject"
    ["Body"] => string(6) "My body of text"
    ["Recipients"] => array(1) {
        [0]
            ["profile"] => string(2) "476"
        }
        [1]
            ["otherusertype"] => string(1) "54"
        }
    }
}

This would enable me to loop through each recipient nice and easily, gaining the user type and the respective ID.

Now, I am currently doing this in Zend Form:

$form->addElement(
    'hidden', 
    $type, 
    array(
        'value' => $id, 
        'belongsTo' => 'Recipients'
    )
);

But this leaves me with

array(4) {
  ["Subject"] => string(4) "hfgh"
  ["Body"] => string(6) "fghfgh"
  ["Recipients"] => array(1) {
    ["profile"] => string(1) "1"
  }
}

As you can see, if I add another recipient of usertype "profile" to the array, it will just be overwritten.

How am I able to get an extra dimension within this array?

Thanks in advance!

1 Answer 1

4

You should set your belongsTo to the array:

    $this->addElement('hidden',  '1', array(
        'value' => 1, 
        'belongsTo' => 'Recipients[profile]'
    ));
    $this->addElement('hidden',  '2', array(
        'value' => 2, 
        'belongsTo' => 'Recipients[profile]'
    ));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This is what I was looking for - not sure why I didn't think of this myself :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.