是否可以将 ZF2 表单用作独立组件?这在 ZF1 中是可能的,但我无法弄清楚 ZF2。
我可以创建一个表单和一个验证器,但无法弄清楚如何呈现表单:
$form = new AddressBookForm('address_book'); \ extends ZendFormForm
if ($this->input->isPost()) {
$validator = new AddressBookValidator(); \ implements ZendInputFilterInputFilterAwareInterface
$form->setInputFilter($validator->getInputFilter());
$form->setData($this->input->getPost());
if ($form->isValid()) {
echo 'valid'; exit;
}
}
// Render form somehow here???
我尝试创建一个视图,但不知道如何为它提供视图助手。谢谢。
我有一个基本的解决方案,似乎可以完成这项工作
$zfView = new ZendViewRendererPhpRenderer();
$plugins = $zfView->getHelperPluginManager();
$config = new ZendFormViewHelperConfig;
$config->configureServiceManager($plugins);
,然后呈现表单
echo $zfView->form()->openTag($form);
echo $zfView->formRow($form->get('name'));
echo $zfView->formSubmit( $form->get('submit'));
echo $zfView->form()->closeTag();
查看此博客。
表单呈现在视图中的文件
你可以简单地通过Zend框架表单视图助手来完成。
$form = $this->form;
$form->prepare();
$this->form()->render($form);
@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 ZendLoader;
use ZendHttpRequest;
use ZendHttpClient;
use ZendCaptcha;
use ZendFormElement;
use ZendFormFieldset;
use ZendFormForm;
use ZendFormFormInterface;
use ZendInputFilterInput;
use ZendInputFilterInputFilter;
use ZendFormViewHelper;
use Common;
class zftest{
function __construct()
{
spl_autoload_register(array($this, '_zftest_autoload'));
}
function _zftest_autoload($class)
{
//
$loader = new ZendLoaderStandardAutoloader(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 ElementEmail('email');
$email->setLabel('Your email address');
$subject = new Element('subject');
$subject->setLabel('Subject');
$subject->setAttributes(array(
'type' => 'text'
));
$message = new ElementTextarea('message');
$message->setLabel('Message');
$captcha = new ElementCaptcha('captcha');
$captcha->setCaptcha(new CaptchaDumb());
$captcha->setLabel('Please verify you are human');
$csrf = new ElementCsrf('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 ZendViewRendererPhpRenderer();
$plugins = $zfView->getHelperPluginManager();
$config = new ZendFormViewHelperConfig;
$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;
}
}
?>
可以使用ZendFormViewHelper
视图帮助程序在视图中呈现窗体。示例:(查看上下文)
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你可以用这个做更多的事情。
更简单的答案帮助我,因为我没有设置服务管理器或视图帮助程序方法。
但匆忙地这对我有用:
$checkbox = new ElementCheckbox('checkbox');
$checkbox->setLabel('Label');
$checkbox->setCheckedValue("good");
$checkbox->setUncheckedValue("bad");
$form = new FormCheckbox();
echo $form->render($checkbox);