2

I'm trying to learn Zend Framework! I'm quite interested in it but I can't find a tutorial which says where it's suppoused to be a Zend_Form class stored! Maybe it's something quite straightforward but I can't get it yet... I've seen tutorials about this:

<?php
 class Form_Example extends Zend_Form
 {
  public function init()
  {
   // Great code here
  }
 }

But none of them said where this code goes????? In a file in which folder in the directory tree?? I've read and I understand and I've done a little example with modules, controllers, actions, layouts and I know the importance about name conventions and the folder structure. So where does this form class must go and how can I call it from a view??

Thanks a lot, I know this must be easy for someone who already knows how to work well with Zend Framework =)

Metafaniel
  • 29,318
  • 8
  • 40
  • 67
  • 1
    If you are using a modular directory structure, then [this tutorial](http://codingexplained.com/coding/php/zend-framework/module-based-forms-directory) will guide you all the way through, assuming that you want to keep forms stored per module. If not, refer to the answers here. :-) – ba0708 Sep 09 '12 at 15:11
  • I didn't find that tutorial earlier, so surely it will help me, thankls a lot, I'll read it =) – Metafaniel Sep 10 '12 at 13:55

3 Answers3

2

I normally have all my forms in a forms folder, alongside the models, controllers, and views.

So, my file structure looks like:

application ->
  configs
  layouts
  plugins
  controllers
  models
  views
  forms ->
    form1.php
    form2.php

Using them in your application isn't quite so simple. You must instantiate the form class in your controller, then pass the form to your view. So in your controller you want something like:

$form1 = new Application_Form_Form1($options);
$request = $this->getRequest();

if($request->isPost()) {
  if($form1->isValid($post))  {
    // form is valid, do form processing here
  }
}

$this->view->form1 = $form1;

Then inside of your view file, you place the form:

<html>
<body>
  <div id="body">
    <?php echo $this->form1; ?>
  </div>
</body>
</html>
Erreth
  • 205
  • 4
  • 16
  • I did something similar with this: `layout()->content ?>` to print in a view something, so basically I need to do it similarly! Thanks for your help ;) – Metafaniel Sep 10 '12 at 13:51
2

The best way to do this is to let ZF do it for you. ZF ships with a command line interface for both windows and *nix.

At the command line you can type zf create form Example, ZF will then create an empty form named Example.php at it's default application level location.

Typically this will be at application/forms/Example.php and the classname will be Application_Form_Example.

If you need to have a form constructed in a module the command would be similar:
zf create form Example -m admin
where -m indicates you want the file created in a module and admin is name of the module.

Forms are one of the predefined resources in Zend Framework and as such have a default location. There are several other resources that are predefined and have defaults.

The Module Resource Autoloader

Zend Framework ships with a concrete implementation of Zend_Loader_Autoloader_Resource that contains resource type mappings that cover the default recommended directory structure for Zend Framework MVC applications. This loader, Zend_Application_Module_Autoloader, comes with the following mappings:

    forms/          => Form
    models/         => Model
    models/DbTable/ => Model_DbTable
    models/mappers/ => Model_Mapper 
    plugins/        => Plugin 
    services/       => Service views/
    helpers         => View_Helper
    filters         => View_Filter 

As an example, if you have a module with the prefix of "Blog_", and attempted to instantiate the class "Blog_Form_Entry", it would look in the resource directory's "forms/" subdirectory for a file named "Entry.php". When using module bootstraps with Zend_Application, an instance of Zend_Application_Module_Autoloader will be created by default for each discrete module, allowing you to autoload module resources.

RockyFord
  • 8,529
  • 1
  • 15
  • 21
  • I use also the command line interface, seems useful! I've created my project with `zf create project` as I red in the book I'm learning to, but I didn't know ZF can create examples for you this way! =O This seems quite useful for someone like me who is still trying to learn how ZF works! THANKS! Quite useful ;) – Metafaniel Sep 10 '12 at 13:54
2

At the heart of your question are the issues of:

  1. autoloading
  2. how the ZF autoloader works in general, and
  3. how the ZF autoloader is configured by default in a standard ZF app

which are actually three distinct, though clearly-related, issues.

Assuming that you have the default ZF installation in which the appnamespace is set to "Application", then name your form class Application_Form_Example and store it in the file application/forms/Example.php.

Then you can instantiate (in a controller, for example) using:

$form = new Application_Form_Example().

Make sure that you have resources.modules[] = in application/configs/application.ini.

For additional discussion about autoloading, see https://stackoverflow.com/a/10933376/131824

Community
  • 1
  • 1
David Weinraub
  • 14,144
  • 4
  • 42
  • 64
  • I've just scratched the surface about those concepts you comment, si indeed I need much more to read and learn and eventually I'll understand everything. This framework it's a little confusing at first but you can smell it's power ;) Thanks for your help! – Metafaniel Sep 10 '12 at 13:52
  • 1
    So true. I found learning ZF to be a steep climb. But once I had more experience, I actually found it to be relatively straightforward. Can't believe I'm actually saying that. ;-) Good luck! – David Weinraub Sep 10 '12 at 14:16