If it helps, here's how I generally have been working with forms in my
action controllers. It has been working for me pretty well so far and uses a
single action.
public function createAction()
{
$form = new Default_Form_User();
$this->view->form = $form;
// Return early if form data was not posted
if (!$this->_request->isPost()) {
return;
}
// Return early if posted data does not validate
if (!$form->isValid($this->_request->getPost())) {
return;
}
// Process form
try {
$this->_processCreateUserForm($form);
$this->_helper->flashMessenger('User has been created');
$this->_helper->redirector( ... );
} catch (Exception $e) {
$this->view->error = "Unable to process process your request, please
try again later".
$this->view->exception = $e;
}
}
What I like about handling forms this way is that if the form doesn't
validate it will be displayed with validation error messages for the user,
along with pre-populating all of the fields with what the user supplied.
If the form does validate, then it passes the form to the form handler
method. Or, the form (or its data) can be passed directly to a model if the
model knows how to handle the form data. Either way, there's no need to
create a new action as you can call any method in the class from within the
class like a normal object.
The redirect at the end helps prevent accidental resubmitting of the form if
the user clicks their "back" button in the browser.
You can get fancy with the try/catch, but normally I haven't needed to. But
that depends on your application.
--
Hector
On Wed, Jul 29, 2009 at 7:41 PM, aSecondWill <[email protected]> wrote:
>
> Hi, Thanks Ralph. Much appreciated.
>
> I ended up string the form in the registry, which seemed to work pretty
> well.
>
> Thanks again, Will
>
>
> Ralph Schindler-2 wrote:
> >
> > _forward will return back to the Action Controller, but will actually
> > create a new instance of the action controller to utilize.
> >
> > At this point you have a couple of options:
> >
> > * use some sort of application registry to house your form object
> >
> > * use a static member of your ActionController class to store the form
> > object
> >
> > * refactor your actions such that common functionality can be found
> > inside protected _doSomeKindOfWOrk() methods that several actions can
> > call out to (this is probably the most ideal solution). You can have
> > different protected methods for different forms you need to process.
> >
> > hope that helps!
> > -ralph
> >
> > aSecondWill wrote:
> >> ok, thanks,
> >>
> >> "accessing the same instance of the form object"
> >>
> >>
> >> hmm. how do you do that? I tried making it a protected var and just
> >> creating
> >> it if it wasn't already, but the whole controller is re-instantiated on
> >> forward.
> >>
> >> is this even the correct way to do multiple forms on a page? seems like
> i
> >> must be missing something as this must be something people do all the
> >> time.
> >>
> >>
> >>
> >>
> >> Peter Warnock-2 wrote:
> >>> On Sun, Jul 26, 2009 at 8:36 PM, aSecondWill <[email protected]>
> >>> wrote:
> >>>
> >>>> If the form dosn't validate, how do i display the original page, with
> >>>> the
> >>>> form errors etc.
> >>>
> >>> _forward back to the original action and make sure you are accessing
> the
> >>> same instance of the form object, it contains the error stack after
> >>> calling
> >>> isValid. - pw
> >>>
> >>>
> >>
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/Seperate-action-to-proccess-a-form-tp24673119p24731298.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
>
>