Zend框架2 - 独立表单

时间:2012-09-24 10:30:06

标签: forms zend-framework2

是否可以将ZF2表单用作独立组件? ZF1可以实现这一点,但我无法用ZF2来解决这个问题。

我可以创建表单和验证器,但无法弄清楚如何呈现表单:

$form = new AddressBookForm('address_book'); \\ extends Zend\Form\Form

if ($this->input->isPost()) {
    $validator = new AddressBookValidator(); \\ implements Zend\InputFilter\InputFilterAwareInterface
    $form->setInputFilter($validator->getInputFilter());
    $form->setData($this->input->getPost());

    if ($form->isValid()) {
        echo 'valid'; exit;
    }
}

// Render form somehow here???

我尝试创建一个视图,但无法弄清楚如何给它提供视图助手。感谢。

5 个答案:

答案 0 :(得分:7)

我有一个基本的解决方案,似乎可以完成这项工作

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

然后呈现表单

echo $zfView->form()->openTag($form);
echo $zfView->formRow($form->get('name'));
echo $zfView->formSubmit( $form->get('submit'));
echo $zfView->form()->closeTag();

答案 1 :(得分:5)

查看此博客。

Form Render in View file

你可以通过zend框架表单视图助手来完成。

$form = $this->form;
$form->prepare();
$this->form()->render($form);

答案 2 :(得分:3)

@ CodeMonkey的方法很好,但代码不完整。我从他和部分代码中找到的其他答案拼凑了一个工作实例。

<?php
    /*
     * @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\Loader;
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;

use \Common;

class zftest{


function __construct()
{

    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()
{



    $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:<pre>' . print_r($address, 1) . '</pre>';
        print '<pre>' . print_r($response->getBody(), 1) . '</pre>';

    }
}

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')) . "<br />\n";
    $output .= $zfView->formRow($form->get('captcha')) . "<br />\n";
    $output .= $zfView->formSubmit( $form->get('send')) . "<br />\n";
    $output .= $zfView->form()->closeTag() . "\n";

    echo $output;

}
}
?>

答案 3 :(得分:2)

您可以使用Zend\Form\View\Helper视图助手在视图中呈现表单。 示例:(查看上下文)

My Form: 
<?php echo $this->form()->openTag($this->form); ?>
<?php echo $this->formCollection($this->form); ?>
<?php echo $this->form()->closeTag($this->form); ?>

注意 $this->form是分配给视图的$form变量。此外,视图帮助程序在视图中始终可用,只要它们注册为invokables(内置帮助程序始终如此)。

这将呈现<form ...> ... </form>标记内的所有元素。 查看其他视图帮助程序以获取更多信息。

另外,请参阅示例文档:http://zf2.readthedocs.org/en/latest/modules/zend.form.quick-start.html 你可以做更多的事情。

答案 4 :(得分:0)

由于我没有设置Service Manager或View Helper方法,所以没有一个简单的答案对我有帮助。

但是匆忙这对我有用:

    $checkbox = new Element\Checkbox('checkbox');
    $checkbox->setLabel('Label');

    $checkbox->setCheckedValue("good");
    $checkbox->setUncheckedValue("bad");

    $form = new FormCheckbox();
    echo $form->render($checkbox);