Quantcast
Channel: LampJunkie.com - Everything related to Linux, PHP, MySQL, Apache, AJAX, Symfony and more
Viewing all articles
Browse latest Browse all 10

Using symfony sfForms in a Zend Application

$
0
0

Recently I have been experimenting with the Zend Framework to look into it’s potential to be used in my future applications. Having been a symfony junkie for the last couple of years I was reluctant to try and use it, but I see that it has come a long way since last using it. I’m glad to see that there is finally the addition of the “zf” command line utility to help scaffold projects. While overall it’s a pretty solid framework, there are many things that I think are left to be desired when compared to symfony. The main thing I feel that is lacking is the form system within Zend.

I have grown to love symfony’s sfForm along with it’s sfWidget and sfValidator systems. I have been making heavy use of extending the sfFormObject class to easily create forms that update and return or create new model objects. Personally, I like to deal with domain objects as much as possible and not have to deal with passing around arrays in my action code.

So, I decided to see how easy it would be to use symfony’s sfForms within a Zend Application. I turns out that it’s really easy to get this working and have the best of both worlds!

For my example, I am going to base it on the Zend Framework Quickstart Documentation. I am going to assume that you have gone through the first couple of steps of creating the project, etc… What follows below is essentially a replacement for the Create a Form step.

Step 1. – Add symfony to your Zend Project

The first thing that you need to do is put the entire symfony core into your library so that you have access to the sfForm, sfWidget, and sfValidator classes, etc… So you simply grab symfony 1.4 from the installation page here.

Once you have the symfony core downloaded, you are going to copy the symfony lib directory to a new symfony directory in your project’s library folder.

At this point you’ll also want to create a Form directory to hold the forms class that you are going to create later on.

So you will have a structure like this:

  1.  
  2. myproject
  3. |– application
  4. |– library
  5. |   |– Form
  6. |   |– symfony
  7. |        |– lib
  8. |             |– action
  9. |             |– addon
  10. |             |– etc
  11. |   |– Zend

Step 2. – Register some Autoloading

Now we need to make sure that Zend will be able to autoload any of the core symfony classes that we are going to use. So we do this by modifying the application Bootstrap class so that it looks like this:

  1.  
  2. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  3. {
  4.     protected function _initSymfonyAutoload()
  5.     {
  6.         require_once 'symfony/lib/autoload/sfCoreAutoload.class.php';
  7.         sfCoreAutoload::register();
  8.     }
  9. }

Now at this point we are ready to use sfForms to our hearts content!

Step 3. – Create a Form

Create a “application/forms” directory if it doesn’t exist and add the following Guestbook.php class:

  1.  
  2. class Application_Form_Guestbook extends sfForm
  3. {
  4.  public function configure()
  5.  {
  6.   $this->setWidgets(array(
  7.                     'email' => new sfWidgetFormInput(array('label' => 'Your email address:')),
  8.                     'comment' => new sfWidgetFormTextarea(array('label' => 'Please Comment:'))
  9.   ));
  10.  
  11.   $this->setValidators(array(
  12.             'email' => new sfValidatorEmail(array('max_length' => 255, 'required' => true)),
  13.             'comment' => new sfValidatorString(array('max_length' => 255, 'required' => true))
  14.   ));
  15.  }
  16. }

Step 4. – Add Form to Controller

Now we add the form instantiation and processing code to the controller action. As you can see this is very similar as using the native Zend forms.

  1.  
  2. class GuestbookController extends Zend_Controller_Action
  3. {
  4.     public function signAction()
  5.     {
  6.         // action body
  7.         $form = new Application_Form_Guestbook();
  8.  
  9.         if($this->getRequest()->isPost()){
  10.          
  11.             $form->bind($this->getRequest()->getPost());
  12.          
  13.             if($form->isValid()){
  14.          
  15.                 // get cleaned, valid form values
  16.                 $values = $form->getValues();
  17.          
  18.                 // map to model, etc…
  19.          
  20.                 return $this->_helper->redirector('index');
  21.             }
  22.         }
  23.        
  24.         $this->view->form = $form;
  25.     }
  26. }

Step 5. – Add Form to View

Finally, we add the form rendering to the sign.phtml file:

  1.  
  2. <form action="<?php echo $this->url(); ?>" method="post">
  3.  
  4.  <table>
  5.   <?php echo $this->form; ?>
  6.  </table>
  7.  
  8.  <input type="submit" value="Save" />
  9.  
  10. </form>

As you can see, getting sfForms to work in Zend is pretty easy once everything is set up. Now you can take advantage of the many neat things you can do with symfony forms.


Viewing all articles
Browse latest Browse all 10

Trending Articles