Magento - Best way to handle forms in blocks?

I come from different MVC frameworks (like Symfony) in Magento. I read a lot about Magento best practices and I see that Magento does not use the typical MVC style. Alan Storm wrote the following:

it’s not the duty of the controller to set the variables for view [...] the controller’s task is to do certain things for the models and then inform the system about the rendering time of the layout.

I think I understand this approach, as it can provide flexibility for blocks.

Right But what about forms?

In a typical MVC structure, you will receive request parameters in the controller, check form data in the controller, do operations with the model (save, load, etc.) or redirect there if necessary, and when everything is clean and tidy, you will provide freshly baked weekends parts to view.

In Magento, all this should happen inside the block, and the (thin) controller should only prepare the layout and then render it. (If I understand.)

I tried to find an article (manual, forum topic, whatever) that describes the steps for creating a separate module with its own new model, which can be edited through a form in the user interface. I would like to see how a user form should work in an interface. I found only general articles about blocks, forms, changes or creating adminhtml forms or setting up contact registration forms or newsletters.

I have done it. Now it works, but I am not satisfied. So, I checked the source code of the Contact form in the main module, and they mixed up the whole picture for me. The built-in contact form uses the IndexController for most of the above operations (almost), like standard MVC.

Can someone suggest me a best practice on how to manage a simple thread like the following? (I have a solution for them below, but I'm not sure if this is the “correct Magento path”):

  • When the page loads, show the form in the block, which is included in a separate page
  • Load the model object from the database with the query parameter
  • Filling in object data in the form
  • When the user submits the form, processes the form data, validates them
  • If the validation fails, display the form again and the error popup
  • If OK, save the data in the database, show the page with gratitude

My confusion is mostly around:

  • Where can I get and manage the request parameter? (I did this in a block class file)
  • And load an object from the database based on this? (Also, and then passed to phtml)
  • How do I pass it to a view if I don't upload it? (I would know a way, but I don't know how best.)
  • Where should form data (POST) be processed, validated and stored? (Block?)
  • How to use redirects in a block? Is redirection necessary, since the gratefully page should be another block / page? Or just an alternative (conditional) view of the same block?
+6
source share
2 answers

You can just look in the Customer / AccountController and see how the loginPost , createPost handle the input of the form.

I would never add CRUD logic to a block. You need to check and process the POST data in your controller. The block should contain only the logic related to viewing: for example, the url of the format or prepare Collection .

Also, the preparation of the form lies on the shoulders of the Controllers. You need to load your object and check it in the Controller action. Then there are several ways to pass this to the block:

  • Mage::register (registry)
  • $this->getLayout->getBlock('your_form_block')->setEntity($object) (setting the variable to lock directly)

Any redirection should only be done in the controller.

UPDATE A few words about why loading the model inside the controller.

  • If you cannot upload your object, this means that the user of the URL (with the object identifier) ​​is out of date, and you may need to redirect the client to some convenient error page.
  • As I said above, all redirects should only be done in the controller. Why is this? Since the block is currently a process, a huge amount of loading work has already been done by Magento - for example, loadLayout and creates all the blocks. You do not want the user to wait all this time to redirect him later.
  • In addition, forwarding anywhere outside the controller makes the application unsupported. Of course, there are exceptions, but you should know what you are doing very well.

You also forgot about another valuable part. If the check is not performed inside your controller, you need to fill out the form with the values ​​submitted by the user. In Zend_Form, this is done fairly well, but with Magento forms you will need to use a session (for example, in AccountController) - save all parts of the key value in the session, and then check the lock for the existence of these session variables. Again, you should only do this if your POST check fails and you redirect the user back to your form. If successful, clear the session variables associated with the form.

As a general tip: if you want to follow Magento's style, read less forums and more kernel code.

+2
source

I'm not purple either, but I think I can answer your questions:

Where can I get and manage the request parameter? (I did this in a block class file) Finds what you need. If you plan to save them in a model, use a controller. If you want to display them on the page, use the block. In most cases, you want to use a controller for this.

And load an object from the database based on this? (Also, and then passed to phtml) If you want to pass the model to the template, you must write a function on your block that will receive the model.

How do I pass it to a view if I don't upload it? (I would know a way, but I don’t know which one is better.) As stated above, create a block with a function that receives the model. Then from your template, you can use $ model = $ this-> functionThatGetsTheModel (); to achieve this function and get the block.

Where should form data (POST) be processed, validated and stored? (Block?) Things like savings models are controls.

How to use redirects in a block? Is redirection necessary, since the gratefully page should be another block / page? Or just an alternative (conditional) view of the same block? It is best to do redirects in your controller. And it is also best to create a new block / template for each page that you plan to make.

About the topic of the block / template: read this page for more information on using the xml layout to use the blocks and template http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-4-magento- layouts-blocks-and-templates

Hope this helps you get started with magento!

0
source

All Articles