Basic Zend_Form by Example with Netbeans

In this post I’m going to show you how to create a simple contact form from scratch in Netbeans in this complete example that shows it  in action.

The great thing about neatbeans is that from version 6.9, it has ZendFramework support which makes life easier for developers.

  • Open up Netbeans

 

  • After you open create a PHP application project
  • Enter the project’s name and setup the source folder(in this case i have the htdocs folder as my source folder, i used XAMPP to configure my Apache server,PHP and Mysql on my system)
  • Select Zend PHP Web Framewok to enable zendframework support. This creates the basic structure of our applicacion.
  • After finishing this basic steps, you’ll have a PHP project with Zendframework support with the following structure..how easy was that.!!!
  • now to create our contact form right click the main node of the project, in this case right click the name of the project “EXAMPLE“, scroll down to ZEND and press Run Command. This will open up a dialog where we can search an execute zendframework commands
  • In the FILTER input box enter FORM, this will search for every command related to FORMS. Select create form from the list and then in the Parameters input box enter the form’s name, in this case it will be contactForm,press run, this will execute this command and add new elements to the structure of your project
  • As we can see, the create form command added a new folder called forms, and inside of it we have our form ContactForm.php. lets create the structure of the form.
  • open up Contactform.php and add the following code in the init() function

    $this->setName('Contact Form');
    
    $title = new Zend_Form_Element_Select('title');
    $title->setLabel('Title')
    ->setMultiOptions(array('mr'=>'Mr', 'mrs'=>'Mrs'))
    ->setRequired(true)->addValidator('NotEmpty', true);
    
    $firstName = new Zend_Form_Element_Text('firstName');
    $firstName->setLabel('First name')
    ->setRequired(true)
    ->addValidator('NotEmpty');
    
    $lastName = new Zend_Form_Element_Text('lastName');
    $lastName->setLabel('Last name')
    ->setRequired(true)
    ->addValidator('NotEmpty');
    
    $email = new Zend_Form_Element_Text('email');
    $email->setLabel('Email address')
    ->addFilter('StringToLower')
    ->setRequired(true)
    ->addValidator('NotEmpty', true)
    ->addValidator('EmailAddress');
    
    $submit = new Zend_Form_Element_Submit('submit');
    $submit->setLabel('Contact us');
    
    $this->addElements(array($title, $firstName,
    $lastName, $email, $submit));
    
    

    We first set the name of the form and then create six elements four text boxes ,one select box and a submit button. As you can see creating an elements its really simple. Each element is of the type Zend_Form_Element_type where type is the type of element we want to create for example Text,Select,Submit,etc.

    After we create the elements,we can configure them the way we want, setting up the label, adding filters and validators, this way we can avoid adding js for basic validation, zendframework takes care of that for us. Another great plus if ZF is that if we have multiple validators you can pass in TRUE as the second parameter to stop processing the form when a particular validation fails

     

  • Finally we add all the elements to the form with the addElements() function.

    To display the form all we have to do is to create an instance of the form in the IndexController.Open up IndexController.php and add the following lines of code

    
    $this->view->mainTitle = "Zend_Form Example with Netbeans 6.9";
    $this->view->formTitle = "<p >Please fill out this form.</p>";
    $form = new Application_Form_ContactForm();
    $this->view->form = $form;
    
    

    after telling the indexcontroller which form is going to render open up index.phtml in view->scripts->index->index.phtml delete the previous lines of code and add the following

    
    <?php echo $this->mainTitle;?>
    <?php echo $this->formTitle;?>
    <?php echo $this->form ;?>
    
    
    To render the form we just echo it.
    Now that the form in done we need to process it when the user submits data, we’re doing this in the indexAction() function. This is what the function will look like after adding the form validation.

$this->view->mainTitle = "Zend_Form Example with Netbeans 6.9";
$this->view->formTitle = "<p >Please fill out this form.</p>";
$form = new Application_Form_ContactForm();
if(!$form->isValid($_POST)){
//process the user input here
echo "Thanks for getting in touch with us";
}
$this->view->form = $form;

Conclusion
as we can see creating a form using zendframork is really simple but when we add Netbeans to the equations, that’s when life becomes more easy, using the ZF command dialog we can really speed up our development.

Advertisements

Posted on January 10, 2011, in ZendFramework and tagged , , . Bookmark the permalink. 3 Comments.

  1. When you use setRequired() validator de form element receive by default de NotEmpty validator, so that’s not necessary. Other thing is that the default value for setRequired() is true, so you could write the code above with the same result:

    $firstName = new Zend_Form_Element_Text(‘firstName’);
    $firstName->setLabel(‘First name’)
    ->setRequired();

  2. Im getting a teeny issue. I cant get my reader to pick-up your feed, Im using bing reader by the way.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: