In demand

ad1

Friday, October 2, 2015

How to Use Zend Framework 2 Forms in Another Script

I have been going back and forth on Zend Framework 2. First it helps me by providing some really robust code. The flip side of the coin is ZF2 makes me crazy because it's always explained terribly by classically educated computer scientist. Given that me and much of the PHP community are autodidacts this is a turn-off.

 I always can tell how useful a PHP framework is by trying to do what I learned first in PHP, building a HTML form. But when it came to ZF2  forms the documentation was missing and not one post or tutorial I could find gave a straight forward answer a to how things worked. What was worse no one gave working code for using ZF2 as a library used by another simple PHP script. Trying to get this done simply and without Zends MVC in place seemed to be unattainable. This meant that ZF2 was not something I would use at first thought. That has changed now that I have this code working. This is a complete stand-alone class use of the Zend framework and only needs to be called.
/*
 * @author Carl McDade
 * 
 * @since 2012-06-11
 * @version 0.2
 *
 */
 
  namespace zftest;

  $path = DOCROOT .'/_frameworks/zf/ZendFramework-2.2.2/library';
  set_include_path(get_include_path() . PATH_SEPARATOR . $path);
  require_once($path . '/Zend/Loader/StandardAutoloader.php');

  use Zend\Http\Request;
  use Zend\Http\Client;

  use Zend\Captcha;
  use Zend\Form\Element;
  use Zend\Form\Fieldset;
  use Zend\Form\Form;
  use Zend\Form\FormInterface;
  use Zend\InputFilter\Input;
  use Zend\InputFilter\InputFilter;
  use Zend\Form\View\Helper;
 
 class zftest{
 
 
 
 function __construct()
 {
          // Call Zend autoloader
          spl_autoload_register(array($this, '_zftest_autoload'));
 }

function _zftest_autoload($class)
{
         //
         $loader = new \Zend\Loader\StandardAutoloader(array('autoregister_zf' => true));
         $loader->registerNamespaces(array('Zend'));

         // finally send namespaces and prefixes to the autoloader SPL
         $loader->register();
         return;
}

 
 
 function zfform()
 {
        

        // Zend Framework 2 form example

        $name = new Element('name');
        $name->setLabel('Your name');
        $name->setAttributes(array(
            'type'  => 'text'
        ));

        $email = new Element\Email('email');
        $email->setLabel('Your email address');

        $subject = new Element('subject');
        $subject->setLabel('Subject');
        $subject->setAttributes(array(
            'type'  => 'text'
        ));

        $message = new Element\Textarea('message');
        $message->setLabel('Message');

        $captcha = new Element\Captcha('captcha');
        $captcha->setCaptcha(new Captcha\Dumb());
        $captcha->setLabel('Please verify you are human');

        $csrf = new Element\Csrf('security');

        $send = new Element('send');
        $send->setValue('Submit');
        $send->setAttributes(array(
            'type'  => 'submit'
        ));


        $form = new Form('contact');
        $form->add($name);
        $form->add($email);
        $form->add($subject);
        $form->add($message);
        $form->add($captcha);
        $form->add($csrf);
        $form->add($send);

        $nameInput = new Input('name');
        // configure input... and all others
        $inputFilter = new InputFilter();
        // attach all inputs

        $form->setInputFilter($inputFilter);

        $zfView = new \Zend\View\Renderer\PhpRenderer();
        $plugins = $zfView->getHelperPluginManager();
        $config  = new \Zend\Form\View\HelperConfig;
        $config->configureServiceManager($plugins);

        $output = $zfView->form()->openTag($form) . "\n";
        $output .= $zfView->formRow($form->get('name')) . "
\n";
        $output .= $zfView->formRow($form->get('captcha')) . "
\n";
        $output .= $zfView->formSubmit( $form->get('send')) . "
\n";
        $output .= $zfView->form()->closeTag() . "\n";

        echo $output;
       
 }
 }
?>
The above code will print out a HTML form when the Class method is called. I am using this class in the Content Connection Kit as an example of how to use it to blend third-party frameworks and CMS together as loosely coupled libraries.

Today's Links

Post a Comment

Print this!