简体   繁体   English

如何在ZF1项目的视图中呈现ZF2表单

[英]How to render ZF2 form in view in ZF1 project

I am trying to use ZF2 classes in ZF1 project. 我正在尝试在ZF1项目中使用ZF2类。 I have been following this link: Use ZF2 in a ZF1 project . 我一直在关注此链接: 在ZF1项目中使用ZF2 I can use ZF2 classes to create model, table model by inheriting AbstractTableGateway. 我可以使用ZF2类通过继承AbstractTableGateway来创建模型,表模型。

I am getting objects on controller. 我在控制器上收到对象。 But when I tries to render those form object on view (phtml). 但是,当我尝试在视图(phtml)上呈现这些表单对象时。 It is throwing error, like this: 它抛出错误,如下所示:

Fatal error: Call to undefined method Zend\Form\Form::openTag() in /var/www/myproject/application/modules/tools/views/scripts/tablesample/add.phtml on line 14

Here is the view file: 这是视图文件:

<?php
use Zend\Form\Form as Form;
$baseUrl = $this->baseUrl();
$formObject = new Form();
// this is demo  print block of form element
$form = $this->form;
$form->setAttribute('action', $baseUrl.'tools/tablesample/add');
$form->prepare();

echo $formObject->openTag($form);
echo $this->formRow($form->get('name'));
echo $this->formRow($form->get('gender'));
echo $this->formRow($form->get('hobby'));
echo $this->formRow($form->get('email'));
echo $this->formRow($form->get('birth'));
echo $this->formRow($form->get('address'));
echo $this->formRow($form->get('direction'));
echo $this->form()->closeTag();

?>

I think ZF1 is not detecting ZF2's form helper or there may be any other way to use ZF2 form helper in ZF1 project. 我认为ZF1没有检测到ZF2的表单帮助器,或者可能还有其他方法在ZF1项目中使用ZF2表单帮助器。

How can I properly render my ZF2 forms in ZF1 application? 如何在ZF1应用程序中正确呈现我的ZF2表单?

I am following this tutorial to create forms: Zend Framework 2 : Extending Zend\\Form to add Select, Multicheckbox, Email,Date, Textarea, and Radio element 我正在按照本教程创建表单: Zend Framework 2:扩展Zend \\ Form以添加Select,Multicheckbox,Email,Date,Textarea和Radio元素

I just need to confirm whether I can use ZF2 forms in ZF1 project or not. 我只需要确认我是否可以在ZF1项目中使用ZF2表单。 If it can be used then please show me the view file for this tutorial. 如果可以使用,请告诉我该教程的视图文件。

While I personally can't offer any advise in integrating ZF2 into ZF1; 虽然我个人无法将ZF2集成到ZF1中提供任何建议; From the ZF2 side of things, the error you are receiving is due to this line : 从ZF2的角度来看,您收到的错误是由于以下原因引起的:

$formObject->openTag($form);

You are trying to use the form object Zend\\Form\\Form to render the open tag of the form. 您正在尝试使用表单对象Zend\\Form\\Form来呈现Zend\\Form\\Form的打开标记。 Instead you need to be using the form view helper ( Zend\\Form\\View\\Helper\\Form ) and then pass in the form ( $form ) as an argument. 相反,您需要使用表单视图助手Zend\\Form\\View\\Helper\\Form ),然后将表单( $form )作为参数传递。

For example 例如

$this->form()->openTag($form);

You are actually doing it right for the forms closing tag (which doesn't need any arguments): 您实际上是在对表单结束标记(不需要任何参数)进行正确处理:

echo $this->form()->closeTag();

Also, I'm unsure of your purpose for $formObject = new Form() ; 此外,我不确定$formObject = new Form()用途; there is not need for a new form instance in the code you posted (and you shouldn't really create new instances in the view like this anyway). 在您发布的代码中不需要新的表单实例(无论如何,您实际上不应在视图中创建新的实例)。

The $this->form() will be caught by the magic __call() of the renderer; $this->form()将被渲染器的魔术__call()捕获; which will then check if there is a view helper registered with the service name form (returning it when its found). 然后它将检查是否有使用服务名称form注册的视图助手(找到后将其返回)。

All the view helpers work in this manor. 所有视图助手都在此庄园中工作。 They are objects that introspect the arguments provided to them and render the appropriate HTML. 它们是对提供给他们的参数进行内省并呈现适当HTML的对象。

With the $this->form() for instance, is used to render the form open tags. 例如,使用$this->form()来呈现表单打开标记。 Another example could be the URL helper ( $this->url('route') ) which would generate a URL form the provided arguments. 另一个示例可能是URL帮助程序( $this->url('route') ),它将从提供的参数生成URL。

I would recommend reading up on the view helper documentation and take a good look at the source code for the already built in view helpers as this will give you a good idea on how they all work. 我建议阅读有关视图帮助器的文档,并仔细查看已经内置的视图帮助器的源代码,因为这将使您更好地了解它们的工作方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM