NYCPHP Meetup

NYPHP.org

[zendframework] Disparity between $_POST and $form->getValues() in Zend Framework.

Anthony Wlodarski anthony at dating2p0.com
Wed Jul 14 18:26:09 EDT 2010


Well I figured out my own issue.  It was a logic and order of evaluation issue.  Basically the field is dynamically populated  if there is no $_POST request already waiting.  When the $_POST request exists based on the order of evaluation the dyanmic form field is never added and therefore never added ot the Zend_Form objects list of validators.

-Anthony

-----Original Message-----
From: "Anthony Wlodarski" <anthony at dating2p0.com>
Sent: Wednesday, July 14, 2010 5:46pm
To: zendframework at lists.nyphp.org
Subject: [zendframework] Disparity between $_POST and $form->getValues() in Zend Framework.

I have been scratching my head on this one.  I have a form that has many elements.  One of them is a listing of blocked users.  I feed the blocked users in after other form elements have been created with the following function:

$form->setBlockedUsers($this->userModel->get_blocked_users());

Function body:

    public function setBlockedUsers($blocked_users = null)
    {
        $element = new Zend_Form_Element_MultiCheckbox('blocked_users');
        $element->setLabel('Remove the following users from your blocked list by selecting the checkbox.');
        $element->addMultiOptions($blocked_users);
        $this->addElement($element);
    }

Now when I go to get the values I have a generic validation method from Zend Framework, then I feed the model the form values as necessary and off it goes to the database:

        if($this->getRequest()->isPost())
        {
            if($form->isValid($_POST))
            {
                // we are going to update the users settings with the values from the form
                if($this->userModel->updateAccountSettings($form->getValues()))
                {

Inside of of the function call I am looking for $values['blocked_users'] based on the code in setBlockedUsers, however I have learned that the array key is not even there.  I did a quick debug output of the values and this is what I get:


Array
(
    [match_frequency] => 1
    [reverse_match_frequency] => 1
    [features_event] => 1
    [message_event] => 1
    [wink_event] => 1
    [favorite_event] => 1
    [not_interested_event] => 1
    [profile_match_event] => 1
    [allow_chat_requests] => 1
    [blocked_users_abc] => Array
        (
            [0] => 1
            [1] => 2
        )

    [edit_user_settings_submit] => Update Account Settings
)
Array
(
    [match_frequency] => 1
    [reverse_match_frequency] => 1
    [features_event] => 1
    [message_event] => 1
    [wink_event] => 1
    [favorite_event] => 1
    [not_interested_event] => 1
    [profile_match_event] => 1
    [allow_chat_requests] => 1
)
The first array is $_POST and the second array is $values.  I have looked at the code multiple times.  Created the array manually without seeding it with calls from the database, removed the field from the function and put it into the Zend_Form init() function.  I even looked at setIgnore() and made sure it was false.  I can't think of anything else at this point.  Any ideas or suggestions?


Anthony Wlodarski
Lead Software Engineer
[http://www.dating2p0.com] Dating 2.0
646 285 0500 x217
anthony at dating2p0.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/zendframework/attachments/20100714/85f36eb6/attachment.html>


More information about the zendframework mailing list