Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-23-2008, 02:47 PM
gon gon is offline
Junior Member
 
Join Date: Jun 2008
Posts: 4
Default Keeping data when doing _forward()

Hi all!

I've got a controller showing a form at indexAction.
The form goes to showresultsAction via POST. This action validates data and makes a _forward() to "index".

The problem is that the validation data is lost, and also view properties: if I set $this->view->foo = "bar", it won't be set after doing _forward.

Where should I keep validation data for it to be passed to the form?

The Registry doesn't look like a good place to me for this.

TIA
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 06-23-2008, 08:47 PM
Member
 
Join Date: Jun 2008
Location: Florida
Posts: 88
Default

Are you sure that forwarding the request is the way you want to handle it? If you definitely want to go with forward, then maybe you could use a combination of Zend_Session and user-defined parameters in the URL.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-24-2008, 08:19 AM
gon gon is offline
Junior Member
 
Join Date: Jun 2008
Posts: 4
Default

Yep, I was missing the point.
Obviously ZF doesn't work this way. I will write a function for validation that will be called if $_POST data is present.

How do you handle this? Do you use an action for showing the form and another for handle the submitted data? My concerns are about repeating code.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 06-24-2008, 09:13 PM
Member
 
Join Date: Jun 2008
Location: Florida
Posts: 88
Default

Every form I've created so far has a method that creates the form and returns the form object. Then within the action method itself (where the form action points) I handle getting that form object, sending the user submitted POST values to the form object, displaying messages to the user, etc...

For example:
Code:
    public function indexAction()
    {
        // get the needed namespaces
        $viewNamespace = new Zend_Session_Namespace('view');

        // get the form and save to a view var
        $this->view->form = $this->getEmailSenderForm();

        // check if the form was submitted via post
        if ($this->_request->isPost()) {
            if ($this->view->form->isValid($this->_request->getPost())) {
                // get the sanitized values from the form object
                $sanitizedValues = $this->view->form->getValues();

                // since the form was submitted send the post values to the form
                // object to persist their values
                $this->view->form->populate($sanitizedValues);
                
                // the form is valid, save to db or whatever else you need to do
            } else {
                // the form is not valid, send the user back to the form with a message
            }
        }
    }

Last edited by jweber : 06-25-2008 at 01:32 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 06-25-2008, 01:00 PM
gon gon is offline
Junior Member
 
Join Date: Jun 2008
Posts: 4
Default

Thanks for the guidance.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 07-08-2008, 03:27 PM
Junior Member
 
Join Date: Jun 2008
Location: earth
Posts: 14
Default

@jweber

I understand most of the code you have so far except what do you do when you hit the part:

'
} else {
// the form is not valid, send the user back to the form with a message
}
'

How do you keep the values submitted and send back to the form/url that they came from?

also...
what is this part for:
// get the needed namespaces
$viewNamespace = new Zend_Session_Namespace('view');
?

Thank you.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 07-09-2008, 04:11 PM
Member
 
Join Date: Jun 2008
Location: Florida
Posts: 88
Default

The method isValid() populates the form with the values passed to it. As you can see in the code I call this before making a decision about where to send the user. I have my forms action point to the action method that is making this decision, so when the user fails the isValid() method I don't actually forward them anywhere, rather I just don't do the DB save, and I do create an error message that is displayed. A fuller, cleaner version of this code would be:

Code:
    public function indexAction()
    {
        // get the needed namespaces
        $viewNamespace = new Zend_Session_Namespace('view');

        // get the form and save to a view var
        $this->view->form = $this->getEmailSenderForm();

        // check if the form was submitted via post
        if ($this->_request->isPost()) {
            if ($this->view->form->isValid($this->_request->getPost())) {
                // get the sanitized values from the form object
                $sanitizedValues = $this->view->form->getValues();
                
                // the form is valid, save to db via a method within this controller
                $this->saveToDb($sanitizedValues);
            } else {
                // the form is not valid, send the user back to the form with a message generated by Zend_Form
                $viewNamespace->arrayErrors = $form->getMessages();
            }
        }
    }
The namespaces section that you ask about is just something I typically do in action controllers. I instantiate an instance of Zend_Session_Namespace for use in holding info that needs to be stored in the session. In this case you can see I stored the error messages generated by Zend_Form. In my header view script I have code that checks $viewNamespace->arrayErrors for any values. If there are values then it displays them in a div at the top of the page.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 07-09-2008, 05:27 PM
Junior Member
 
Join Date: Jun 2008
Location: earth
Posts: 14
Default

thank you very much for the more detailed explanation, this does help a little.

So you display and process forms in the same action?
You just check to see if the form was posted and valid before processing it within the action?


If i move it out of the if (isValid) check, does $this->view->form->getValues() only work if the form isValid?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 07-09-2008, 05:34 PM
Member
 
Join Date: Jun 2008
Location: Florida
Posts: 88
Default

Yes, I display and process the form data all in the same action. Within the action are calls to private methods within the controller that handle creating the form (which also validates and filters the form data), interacting with the DB, etc...

$this->view->form->getValues() returns the values entered into the form after they've been put through the filters and validation specified when the form is built.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 07-09-2008, 06:14 PM
Junior Member
 
Join Date: Jun 2008
Location: earth
Posts: 14
Default

Where / How are the values getting to '$sanitizedValues'? I see from the $form object, but i cannot seem to retrieve them individually. There is a hidden value in my form that doesnt seem to be included in the values sent. Also, none of the values even seem to show when I print the '$santizedValues' array.

What am I missing here?

Maybe this will help a bit.

I have one action for addition of phone numbers. within that action it checks the params sent and if there is a value of 'number/new' it just shows the form. if 'number/edit/id/xxx' is sent instead of 'number/new', the id of that number is to be added to a hidden element and also the details of that record are initially populated in the fields. This all works up until i submit it. On an 'number/edit', the results I submit are retained in the fields, but I cannot access the hidden 'id' to check if it is set and add that element when the form is rerenedred.

thank you for your advice and time.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 05:21 AM.