|
|||
|
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 |
|
|||
|
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.
|
|
|||
|
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. |
|
|||
|
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. |
|
|||
|
@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. |
|
|||
|
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();
}
}
}
|
|
|||
|
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? |
|
|||
|
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. |
|
|||
|
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. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|