I am using Zend 1.12 and php 5.4.3 and flashMessenger->getMessages() has suddenly stopped working in a controller action.
In AController, a certain type of account is created and it takes 9 steps to create it, so I have 9 actions create1-9Action
On each step I pass the form data to the next step using flashmessenger.
This is the typical structure of an action:
public function create5Action()
{
$form = new My_Form();
$messages = $this->_helper->flashMessenger->getMessages();
$data = $messages[0];
if ($this->_request->isPost())
{
if ($form->isValid($this->_request->getPost()))
{
/* form treatment */
$this->_helper->flashMessenger->addMessage($data);
$this->_redirect($this->_helper->url("create6", "A", null)); // redirect to next step
}
}
$this->_helper->flashMessenger->addMessage($data);
$this->view->form = $form;
}
In this action (create5) the data is intact when arriving from create4Action, it is intact when adding it as a message before $this->view->form = $form;, but when I add new elements to the form and submit it, $messages = $this->_helper->flashMessenger->getMessages(); is null and I do not know why, since it is working for all other actions.
You may have missed adding the else in the isPost() loop.
public function create5Action()
{
$form = new My_Form();
$messages = $this->_helper->flashMessenger->getMessages();
$data = $messages[0];
if ($this->_request->isPost())
{
if ($form->isValid($this->_request->getPost()))
{
/* form treatment */
$this->_helper->flashMessenger->addMessage($data);
$this->_redirect($this->_helper->url("create6", "A", null)); // redirect to next step
}
} else {
$this->_helper->flashMessenger->addMessage($data);
$this->view->form = $form;
}
}
However this is not the anticipated use of the flash messenger. I think a part of your problem is that every time a request is made the flash messenger namespace is cleared during the postDispatch() portion of the dispatch loop.
You may be better off using your own Zend_Session_Namespace instance instead of relying on the instance that flash messenger is using.
public function create5Action()
{
$session = new Zend_Session_Namespace('data');//set elsewhere and forwarded and can be persistent
$form = new My_Form();
$data = $session->data;
if ($this->_request->isPost())
{
if ($form->isValid($this->_request->getPost()))
{
/* form treatment */
$session->newData = $newData;//forward data if needed, old data will persist as you require
$this->_redirect($this->_helper->url("create6", "A", null)); // redirect to next step
}
} else {
$this->view->form = $form;
}
}
Related
I'm trying to achieve a thank you page after submitting a form in Zend 1.12
in the index i have form and i want if the validation passes then should go to another view (NOT INDEX) for thank you page. how can i do this in my code:
public function indexAction()
{
// action body
$C_form = new Application_Form_Eform();
if ($this->_request->isPost()) {
$formData = $this->_request->getPost();
if ($C_form->isValid($formData)) {
$this->_helper->redirector('','result');
exit;
} else {
$C_form->populate($formData);
}
}
$this->view->form = C_eform;
}
and after that where should i create the .phtml file? in the application\views\scripts\index?
I think you're looking for render:
$this->view->render('index/yourotherview.phtml');
In this case, index/ is referring to your views/scripts/index folder, and the yourotherview.phtml file.
So, all together it would be:
public function indexAction()
{
// action body
$C_form = new Application_Form_Eform();
if ($this->_request->isPost()) {
$formData = $this->_request->getPost();
if ($C_form->isValid($formData)) {
$this->_redirect('/index/result);
} else {
$C_form->populate($formData);
}
}
$this->view->form = C_eform;
}
EDIT:
From your comment it looks like you just want to be redirected instead of displaying a different view. In this case, it's as easy as creating a new action and making the view for it:
public function resultAction() {
// Code here if you need it
}
Then create the file result.phtml in the index/ views directory, and you'll need $this->_redirect('/index/result'); in the index controller. (See above code)
I'm struggling with the proper organisation of a (MVC) Controller for my validation of a multipaged form. The problem is that I not only have to check whether the user input is existant at all, but I have to match it with different databases (depending on the field), too. I also need that database-data that results from the user-input for different view-options.
It's unclear for me where I should put that validation at all. I would like to put not too detailed things into the controller, but at the same time I don't like models using each other (without the controller).
Here's a basic example from a controller, the steps stand for different stages/pages of the form:
// GET VARIABLES FROM $_POST + $_GET
private function make_environment()
{
// PUT ALL VARIABLES INTO ARRAY
$vars = array();
if(!empty($_GET)) { $values = array_merge($vars,$_GET); }
if(!empty($_POST)) { $values = array_merge($vars,$_POST); }
// PUT ALLOWED VARIABLES INTO PROPERTY
foreach($this->properties as $property)
{
if(isset($values[$property]))
{
$this->properties[$property] = htmlspecialchars(trim($values[$property]));
}
}
}
// HANDLE DATA
// PRODUCES DATA FOR VIEW
private function set_data()
{
$data = '';
// CHOOSE DATA-OBJECT AND DATA-HANDLER
switch($this->properties['step'])
{
case 1:
// DATA HANDLER
$handler = new calendar($this->properties);
$data['calendar'] = $handler->return_data();
break;
case 2:
// DATA HANDLER
$handler = new form($this->properties);
$data['form'] = $handler->return_data();
break;
}
return $data;
}
// CREATE OR UPDATE VIEW
private function run_view($data)
{
new view('header','');
switch($this->properties['step'])
{
default:
new view('chooser','');
break;
case 1:
new view('calendar',$data['calendar'],$this->properties);
break;
case 2:
new view('form',$data['form'],$this->properties);
break;
}
new view('footer','');
}
At the moment the different 'handlers' (in set_data()) are querying the database, but prior to that I'd need a check whether the required fields have been submitted and whether those fields are valid (whether they exist in the database). I don't really know where to put that kind of validation. Propably in a separate model, but then I'd have to query the DB there and in the data-handler again. Maybe you got an idea ?!
I have no clue about the MVC you use, but I'd prefer something like this:
$form = new MultipageForm('BubblePagesForm');
$form->importFromSession($app->getSession());
if ($form->validates($app->getRequest()))
{
$form->processRequest($app->getRequest());
$form->exportToSession($app->getSession());
$responseType = new SuccessfullFormRequest($form);
}
else
{
$responseType = new InvalidFormRequest($form);
}
$app->setResponse($responseType, array($form));
I'm wondering if there was a way to add a group of elements to a zend form as if they were one element, I guess much like a subform, but it seems the functionality of a subform may be too much...
Here's my use-case. I've created a class that handles multi-page forms. I want to be able to write logic to change the buttons at the bottom of the form based on the page of the form I'm on.
I originally thought that Zend-Form-DisplayGroup would fix my problem, but you have to add the items to the form first and then add them to the display group and can't pass a display group through a function with attached elements. I would like to have a function that would be something like
public function setSubmitButtonGroup($submitButtonGroupElements)
{
/*Code to set button group*/
}
The idea of using an array of elements just hit me right now as opposed to something else and add logic to add that array of elements to the form on render... but does anyone have any "better" ideas or done this before?
BTW, if anyone is wondering... I'm loosely basing my initial design off of this section: Zend Framework Advance Form Usage.
Not sure I understand your problem correctly but this how I do some things.
In a Zend_Form object you can add elements as a group with `addElements($elements) in an array. For the Submit button etc. I have a class where I get the $elements array from and then I simply pop it in. I also add a displayGroup but separately and simply to control where the buttons are. Because a form is an object you can do simple things like the following but I always add a reference to show my intent.
update: shuffled the button manipulation
function addButtons(&$form,$nextName = null) {
$buttons = $this->getButtons(); // this will be an array with your buttons
// make sure you have the element names in your buttons arrays
if ( is_string($nextName) ) {
$buttons['nextButton']->setLabel($nextName);
} elseif ( is_bool($nextName) && false === $nextName ) {
unset($buttons['nextButton'];
}
// repeat for other buttons
$form->addElements($buttons);
$elementNames = array_keys($buttons);
$form->addDisplayGroup($elementNames,'buttonGroup',array('legend'=>'Click some buttons'));
}
$this->addButtons($form,'Finish');
You could make yourself a factory that receive three params, your form element, the current controller and the current action. Then in that factory, you could call a builder based on the controller/action combination and you pass your form.
In your builder you add 1, 2 or 3 buttons based on the corresponding controller/action requirement which are stored in diffrent components. Once it is done, you return your form to the factory and the factory return the form.
My_Form // your Zend_Form object
My_Form_Factory // Your new factory (see below)
My_Form_Factory_Builder_Controller_Action // One of your builder (see below)
My_Form_Factory_Component // Extends the corresponding Zend_Form_Elements
// basic factory that can be called like My_Factory::factory($form, $controller, $action)
class My_Form_Factory {
static public function factory($form, $controller, $action)
$builderClass = "My_Form_Factory_Builder_" . $controller . '_' . $action;
$builder = new $builderClass($form);
return $builder->buildForm();
}
// Basic builder
class My_Form_Factory_Builder_Controller_Action
{
protected $_form;
protected $_previousComponent ;
protected $_nextComponent ;
protected $_cancelComponent ;
public function __construct($form)
{
$this->_form = $form;
$this->_previousComponent = new My_Form_Factory_Component_Previous();
$this->_nextComponent = new My_Form_Factory_Component_Next();
$this->_cancelComponent = new My_Form_Factory_Component_Cancel();
}
public function buildForm()
{
$this->_form->addElement($previousCompnent);
$this->_form->addElement($nextComponent);
$this->_form->addElement($cancelComponent);
return $this->_form;
}
}
If you want to automatize the instanciation you could initialize all the different compoments you might require in an abstract class and in the method buildForm() only add the elements you need for that current interface. (I would rather repeat the code in each builder than rely on this kind of "magic" but it a viable method to do it).
So the complexity of my problem comes with knowing what page of the multipage form. Using an array and the above mentioned addElements() helped.
Simple Answer
The answer to my problem was an array that could be manipulated after the form was "built" so to speak but before it was rendered so that I could add to the form using addElements().
Long Answer
To get the whole picture, imagine each time you hit the next or previous button, you are traversing through an array of subforms. In this case one would need a function to handle the button rendering. I ended up using a case statment, though it's not the best implementation in the world (not reusable in the parent class Form_MultiPage), but it worked:
in my extention of my mulipage form class I have
public function setSubmitControls()
{
$previous = new Zend_Form_Element_Submit('previous',array(
'label'=>'previous',
'required'=>false,
'ignore'=>false,
'order'=>9000
));
$cancel = new Zend_Form_Element_Submit('cancel',array(
'label'=>'Cancel',
'required'=>false,
'ignore'=>false,
'order'=>9003
));
$next = new Zend_Form_Element_Submit('next',array(
'label'=>'Next',
'required'=>false,
'ignore'=>false,
'order'=>9002
));
$finished = new Zend_Form_Element_submit('finish',array(
'label'=>'Finish',
'required'=>false,
'ignore'=>false,
'order'=>9004
));
$submitControls = array();
echo var_dump($this->getCurrentSubForm()->getName());
switch($this->getCurrentSubForm()->getName())
{
case 'billInfo':
$submitControls = array(
$next,
$cancel
);
break;
case 'payerInfo':
$submitControls = array(
$previous,
$next,
$cancel
);
break;
//So on for other subforms
}
$this->setSubmitButtonGroup($submitControls);
}
In my parent class, Form_Multipage, I have
public function setSubmitButtonGroup(array $elements)
{
$this->_submitButton = $elements;
}
And
public function addSubmitButtonGroupToSubForm(Zend_Form_SubForm $subForm)
{
$subForm->addElements($this->_submitButton);
return $subForm;
}
Which is called when I render the "page" of the form with this function
public function prepareSubForm($spec)
{
if (is_string($spec)) {
$subForm = $this->{$spec};
} elseif ($spec instanceof Zend_Form_SubForm) {
$subForm = $spec;
} else {
throw new Exception('Invalid argument passed to ' .
__FUNCTION__ . '()');
}
$subform = $this->setSubFormDecorators($subForm);
$subform = $this->addSubmitButtonGroupToSubForm($subForm);
$subform = $this->addSubFormActions($subForm);
$subform->setMethod($this->getMethod());
return $subForm;
}
I have following action to display a form
public function showformAction() {
$this->view->form = new Form_MyForm();
$this->view->form->setAction( 'submitform' );
}
above action shows a form successfully with only one textarea and submit button.
And I am using following action to submit above form:
public function submitformAction() {
$form = new Form_MyForm();
$request = $this->getRequest();
if ( $request->isPost() ) {
$values = $form->getValues();
print_r($values);die();
} else {
echo 'Invalid Form';
}
}
Above action is showing following output:
Array ( [myfield] => )
It means it is not posting values correctly and always shows empty array or I am not getting posted values correctly. How to post values to submitformAction().
Thanks
I think you must use the isValid() before accessing the values of a submitted form, because it's right there that the values are checked and valorized
public function submitformAction() {
$form = new Form_MyForm();
$request = $this->getRequest();
if ( $request->isPost() ) {
if ($form->isValid( $request->getPost() )) {
$values = $form->getValues();
print_r($values);die();
}
} else {
echo 'Invalid Form';
}
}
In complement to #VAShhh response. With some more details:
You need to do two things, populate your form fields with the POSTed data and applying security filters and validators to that data. Zend_Form provides one simple function which perform both, it's isValid($data).
So you should:
build your form
test you are in a POST request
populate & filter & validate this data
either handle the fact in can be invalid and re-show the form wich is now
decorated with Errors OR retrieve valid data from the form
So you should get:
function submitformAction() {
$form = new Form_MyForm();
$request = $this->getRequest();
if ( $request->isPost() ) {
if (!$form->isValid($request->getPost())) {
$this->view->form = $form;
// here maybe you could connect to the same view script as your first action
// another solution is to use only one action for showform & submitform actions
// and detect the fact it's not a post to do the showform part
} else {
// values are secure if filters are on each form element
// and they are valid if all validators are set
$securizedvalues = $form->getValues();
// temporary debug
print_r($securizedvalues);die();
// here the nice thing to do at the end, after the job is quite
// certainly a REDIRECT with a code 303 (Redirect after POSt)
$redirector = $this->_helper->getHelper('Redirector');
$redirector->setCode(303)
->setExit(true)
->setGotoSimple('newaction','acontroller','amodule');
$redirector->redirectAndExit();
} else {
throw new Zend_Exception('Invalid Method');
}
}
And as said in the code re-showing the form you shoudl really try to use the same function for both showing and handling POST as a lot of steps are really the same:
building the form
showing it in the view in case of errors
By detecting the request is a POST you can detect you are in the POST handling case.
Once you're OK with basic record form built after example from Tutorial, you realize you want more professionally designed Record Form. E.g. I don't want to duplicate record form for the same table in User and Admin areas.
1) Does anyone use some mechanism, possibly inheritance, to reduce duplication of almost similar admin and user forms? Is that burdensome or sometimes you better just do with copy-pasting?
2) Has anyone considered it to be a good idea to build some basic Record class
that can determine that among several record forms on this page, the current post is addressed specifically to this record form
that can distinguish between Edit or Delete buttons clicks in some organized fashion.
3) My current practice includes putting all form config code (decorators, validations, initial values) into constructor and form submit handling is put into a separate ProcessSubmit() method to free controller of needless code.
All the above addresses to some expected Record Form functionality and I wonder if there is any guideline, good sample app for such slightly more advanced record handling or people are still reinveting the wheel. Wondering how far you should go and where you should stop with such impovements...
Couple of suggestions:
First of all - Use the init() function instead of constructors to add your elements when you are subclassing the form. The init() function happens after the parameters you pass to the class are set.
Second - Instead of subclassing your form - you can just set an "option" to enable the admin stuff:
class My_Record_Form extends Zend_Form {
protected $_record = null;
public function setRecord($record) {
$this->_record = $record;
}
public function getRecord() {
if ($this->_record === null || (!$this->_record instanceOf My_Record)) {
throw new Exception("Record not set - or not the right type");
}
return $this->_record;
}
protected $_admin = false;
public function setAdmin($admin) {
$this->_admin = $admin;
}
public function getAdmin() { return $this->_admin; }
public function init() {
$record = $this->getRecord();
$this->addElement(......);
$this->addElement(......);
$this->addElement(......);
if ($this->getAdmin()) {
$this->addElement(.....);
}
$this->setDefaults($record->toArray());
}
public function process(array $data) {
if ($this->isValid($data)) {
$record = $this->getRecord();
if (isset($this->delete) && $this->delete->getValue()) {
// delete button was clicked
$record->delete();
return true;
}
$record->setFromArray($this->getValues());
$record->save();
return true;
}
}
}
Then in your controller you can do something like:
$form = new My_Record_Form(array(
'record'=>$record,
'admin'=>My_Auth::getInstance()->hasPermission($record, 'admin')
));
There is nothing "wrong" with making a My_Record_Admin_Form that handles the admin stuff as well - but I found this method keeps all the "record form" code in one single place, and a bit easier to maintain.
To answer section 2: The edit forms in my code are returned from a function of the model: $record->getEditForm() The controller code ends up looking a little like this:
protected $_domain = null;
protected function _getDomain($allowNew = false)
{
if ($this->_domain)
{
return $this->view->domain = $this->_domain;
} else {
$id = $this->_request->getParam('id');
if (($id == 'new' || $id=='') && $allowNew)
{
MW_Auth::getInstance()->requirePrivilege($this->_table, 'create');
$domain = $this->_table->createRow();
} else {
$domain = $this->_table->find($id)->current();
if (!$domain) throw new MW_Controller_404Exception('Domain not found');
}
return $this->view->domain = $this->_domain = $domain;
}
}
public function editAction()
{
$domain = $this->_getDomain(true);
MW_Auth::getInstance()->requirePrivilege($domain,'edit');
$form = $domain->getEditForm();
if ($this->_request->isPost() && $form->process($this->_request->getPost()))
{
if ($form->delete && $form->delete->getValue())
{
return $this->_redirect($this->view->url(array(
'controller'=>'domain',
'action'=>'index',
), null, true));
} else {
return $this->_redirect($this->view->url(array(
'controller'=>'domain',
'action'=>'view',
'id'=>$form->getDomain()->id,
), null, true));
}
}
$this->view->form = $form;
}
So - the actual id of the record is passed in the URI /domain/edit/id/10 for instance. If you were to put multiple of these forms on a page - you should make sure to set the "action" attribute of the form to point to an action specific to that form.
I created a SimpleTable extends Zend_Db_Table and SimpleForm extends Zend_Db_Form classes. Both of these assume that your table has an auto-incrementing ID column.
SimpleTable has a saveForm(SimpleForm $form) function which uses the dynamic binding to match form element names to the columns of the record. I also included an overridable saveFormCustom($form) for any special handling.
The SimpleForm has an abstract setup() which must be overridden to setup the form. I use the init() to do the initial setup (such as adding the hidden ID field).
However, to be honest, I really don't like using the Zend_Form object, I feel like that should be handled in the View, not the Model or Controller.