我的zend框架2项目涉及一个在线餐厅菜单,我正在尝试制作一个表单,将Pizzas添加到数据库中。但是我的代码有问题。表单不会显示,而是出现此错误:
在表单
中找不到[pizza_name]名称的元素
请帮我查一下我的代码有什么问题。
以下是我的文件:
addPizzaForm.php:
<?php
namespace Pizza\Form;
use Zend\Form\Form;
use Pizza\Form\AddPizzaForm;
use Pizza\Model\Pizza;
class AddPizzaForm extends Form
{
public function construct() {
parent:: construct('addpizzaform');
$this->add(array(
'name' => 'pizza_name',
'type' => 'text',
'options' => array(label => 'Pizza name')));
$this->add(array(
'name' => 'ingredients',
'type' => 'textarea',
'options' => array(label => 'Ingredients')));
$this->add(array(
'name' => 'small_price',
'type' => 'text',
'options' => array(label => 'Small price')));
$this->add(array(
'name' => 'big_price',
'type' => 'text',
'options' => array(label => 'Big price')));
$this->add(array(
'name' => 'family_price',
'type' => 'text',
'options' => array(label => 'Family price')));
$this->add(array(
'name' => 'party_price',
'type' => 'text',
'options' => array(label => 'Party price')));
$this->add(array(
'name' => 'add_pizza',
'type' => 'submit',
'attributes' => array(
'value' => 'Add New Pizza',
'id' => 'submitbutton')));
}
}
add.phtml查看:
<?php
echo $this->headTitle('Add new Pizza');
?>
<div class="row">
<div class="col-md-8"> <h1> Add new Pizza </h1>
</div>
<?php
$form = $this->form;
$form->setAttribute('action', $this->url('pizza',array('action'=>'add')));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formRow($form->get('pizza_name')) . "</br>";
echo $this->formRow($form->get('ingredients')) . "</br>";
echo $this->formRow($form->get('small_price')) . "</br>";
echo $this->formRow($form->get('big_price')) . "</br>";
echo $this->formRow($form->get('family_price')) . "</br>";
echo $this->formRow($form->get('party_price')) . "</br>";
echo $this->formRow($form->get('add_pizza')) . "</br>";
echo $this->form()->closeTag();
?>
PizzaController.php:
<?php
namespace Pizza\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Pizza\Form\AddPizzaForm;
use Pizza\Model\Pizza;
class PizzaController extends AbstractActionController {
protected $pizzaTable;
public function addAction()
{
$add_form = new AddPizzaForm();
$request = $this->getRequest();
if($request->isPost())
{
$pizza = new Pizza();
$add_form->setInputFilter($pizza->getInputFilter());
$add_form->setData($request->getPost());
if($form->isValid())
{
$pizza->exchangeArray($form->getData());
$this->getPizzaTable()->save($pizza);
}
return $this->redirect()->toRoute('pizza');
}
return array('form' => $add_form);
}
public function getPizzaTable()
{
if( !$this->pizzaTable)
{
$sm = $this->getServiceLocator();
$this->pizzaTable = $sm->get('Pizza\Model\PizzaTable');
}
return $this->pizzaTable;
}
}
答案 0 :(得分:6)
欢迎来到stackoverflow!
您收到了这些奇怪的错误,因为有四个需要关注的重要细节。
A - construct()
中的AddPizzaForm
方法名称和签名错误。您必须将construct
重命名为__construct
更改内容,如下所示:
class AddPizzaForm extends Form
{
public function __construct($name = null, $options = array() )
{
$formName = is_null($name) ? 'addpizza-form' : $name;
parent::__construct($formName, $options);
}
}
B - 在表单中创建一个init()
方法,并在其中添加表单元素。 official documentation:
如果您通过扩展Zend \ Form \ Form创建表单类,那么 不能在__construct中添加自定义元素,而是在init()方法中添加。
所以,在你的情况下:
class AddPizzaForm extends Form
{
// constructor etc..
public function init()
{
$this->add(array(
'name' => 'pizza_name',
'type' => 'text',
'options' => array(label => 'Pizza name')
)
);
// ... add other for elements ...
}
}
C。最后,在您的控制器中,您尝试手动实例化您的表单:
$add_form = new AddPizzaForm();
这也是不好的做法。您需要从ServiceManager获取AddPizzaForm
实例。这个细节也在文档中说明:
你不能直接实例化你的表单类,而是得到一个 它的实例通过Zend \ Form \ FormElementManager。
为此,请在AddPizzaForm
中介绍module.config.php
,如下所示:
'form_elements' => array(
'invokables' => array(
'add-pizza-form' => 'Pizza\Form\AddPizzaForm',
)
)
然后在控制器中抓住它:
$add_form = $this->getServiceLocator()->get('FormElementManager')->get('add-pizza-form');
D - 最后一件事是;在进行一些实验之前,请先阅读大部分documentation第一个。阅读文档是在任何语言或框架中取得更好成绩的最重要的一步。
希望它有所帮助。快乐的编码!