Popular just now

ad1

How to Use Zend Framework 2 Forms in Another Script (Factory)

Here's the same stand-alone form class for Zend Framework 2. I am posting this script as-is from the Content Connection Kit so you'll need to modify it to remove errors of omission. The demo url is http://fhqk.com/cck/?zftest/zfform_factory.

Be aware that I have found that ZF2's take-over of session handling means that any use of session_start() in the container script will cause ZF2 to fail with fatal error. This means that the use of session_start() for other frameworks or class libraries like PFBC running in the same scope will block ZF2 from loading. Because of things like this incompatibility with other systems like Drupal 7.x< 8.x and Symfony2 can be expected. You can work around the problem of ZF2 sessions by avoiding the use of captcha and other functionality that is dependant on sessions. But if anyone knows of a way to disable/override ZF2 session handling completely please speak up!

common = Common::get_instance();
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 zftest()
{

global $cck;

print_r($cck->_query_segment());

$uri = 'http://maps.google.com/maps/api/geocode/json';
$address = urlencode('berlin');
$sensor = 'false';

$request = new Request();
$request->setUri($uri);
$request->setMethod('GET');

$client = new Client($uri);
$client->setRequest($request);
$client->setParameterGet(array('sensor'=>$sensor,'address'=>$address));
$response = $client->dispatch($request);  
if ($response->isSuccess()) {
print 'Your Request for:
' . print_r($address, 1) . '
'; print '
' . print_r($response->getBody(), 1) . '
'; } } function zfform() { global $cck; // get all links from each class controller $menu = $cck->_hooks('hook_links'); $sub_menu = $cck->_hooks('hook_module_links'); $variables['navigation'] = $cck->_menu_links($menu, 'links_main_menu', ' | '); $variables['page_title'] = 'Zend Framework Tests'; $variables['content_title'] = 'Example Form'; // 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('message')) . " \n"; $output .= $zfView->formRow($form->get('captcha')) . " \n"; $output .= $zfView->formSubmit( $form->get('send')) . " \n"; $output .= $zfView->form()->closeTag() . "\n"; $variables['content'] = $output; print $cck->_view('default', $variables); } function zfform_factory() { global $cck; // get all links from each class controller $menu = $cck->_hooks('hook_links'); $variables['navigation'] = $cck->_menu_links($menu, 'links_main_menu', ' | '); $variables['page_title'] = 'Zend Framework Tests'; $variables['content_title'] = 'Example Form Factory'; // Zend framework 2 forms factory example $factory = new Factory(); $form = $factory->createForm(array( 'hydrator' => 'Zend\Stdlib\Hydrator\ArraySerializable', 'elements' => array( array( 'spec' => array( 'name' => 'name', 'options' => array( 'label' => 'Your name', ), 'attributes' => array( 'type' => 'text' ), ) ), array( 'spec' => array( 'type' => 'Zend\Form\Element\Email', 'name' => 'email', 'options' => array( 'label' => 'Your email address', ) ), ), array( 'spec' => array( 'name' => 'subject', 'options' => array( 'label' => 'Subject', ), 'attributes' => array( 'type' => 'text', ), ), ), array( 'spec' => array( 'type' => 'Zend\Form\Element\Textarea', 'name' => 'message', 'options' => array( 'label' => 'Message', ) ), ), array( 'spec' => array( 'type' => 'Zend\Form\Element\Captcha', 'name' => 'captcha', 'options' => array( 'label' => 'Please verify you are human.', 'captcha' => array( 'class' => 'Dumb', ), ), ), ), array( 'spec' => array( 'type' => 'Zend\Form\Element\Csrf', 'name' => 'security', ), ), array( 'spec' => array( 'name' => 'send', 'attributes' => array( 'type' => 'submit', 'value' => 'Submit', ), ), ), ), /* If we had fieldsets, they'd go here; fieldsets contain * "elements" and "fieldsets" keys, and potentially a "type" * key indicating the specific FieldsetInterface * implementation to use. 'fieldsets' => array( ), */ // Configuration to pass on to // Zend\InputFilter\Factory::createInputFilter() 'input_filter' => array( /* ... */ ), )); $form->prepare(); // Assuming the "contact/process" route exists... $form->setAttribute('action', 'contact/process'); // Set the method attribute for the form $form->setAttribute('method', 'post'); $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->formRow($form->get('security')) . " \n"; $output .= $zfView->formRow( $form->get('subject')) . " \n"; $output .= $zfView->formSubmit( $form->get('send')) . " \n"; $output .= $zfView->form()->closeTag() . "\n"; $variables['content'] = $output; print $cck->_view('default', $variables); } } ?>

No comments:

Post a Comment