简单表单未呈现,它会引发错误:
类型错误:参数1传递给
Symfony\Component\Form\FormRenderer::renderBlock()必须是实例
Symfony\Component\Form\FormView的,未给定,已调用
C: \Development\Projects\ShopStock\var\cache\dev\twig\14\144a2b61bf8b4b2e2549f7da9c9acb553cbf793088729905b2adf9cd03dbe799。php
在线103
ProductForm。php:
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
/**
* Product Form
*/
class ProductForm extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add("ean", TextType::class, array(
"label" => "product.ean"
));
$builder->add("name", TextType::class, array(
"label" => "product.name"
));
}
}
namespace App\Controller;
use App\Entity\Product;
use App\Form\ProductForm;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Translation\TranslatorInterface;
/**
* Product Controller
*/
class ProductController extends Controller
{
/**
* @var TranslatorInterface
*/
private $_translator;
/**
* @param TranslatorInterface $translator
*/
public function __construct(TranslatorInterface $translator)
{
$this->_translator = $translator;
}
/**
* Edit
*
* @ParamConverter("product", class="App\Entity\Product")
* @Route("product/{slug}/edit", name = "product-edit")
* @Method({"GET", "POST"})
* @Template(template = "product/product_edit.html.twig")
*
* @param Request $request
* @param Product $product
*
* @return array
*/
public function edit(Request $request, Product $product)
{
$form = $this->createEditProductForm($product);
// check for POST request
if ($request->isMethod(Request::METHOD_POST)) {
$form->handleRequest($request);
// check validation
if ($form->isValid()) {
// ....
}
}
return array(
"form" => $form->createView(),
"product" => $product
);
}
/**
* Create Edit Product Form
*
* @param Product $product
* @return FormInterface
*/
private function createEditProductForm(Product $product)
{
$form = $this->createForm(ProductForm::class, $product, array(
"action" => $this->generateUrl("product-edit", array("slug" => $product->getSlug())),
"method" => Request::METHOD_POST
));
$form->add("submit", SubmitType::class, array(
"label" => $this->_translator->trans("save", array(), "form")
));
return $form;
}
}
创作者json:
"require": {
"php": "^7.0.8",
"ext-iconv": "*",
"portphp/csv": "^1.0",
"portphp/doctrine": "^1.0",
"sensio/framework-extra-bundle": "^5.1",
"stof/doctrine-extensions-bundle": "^1.3",
"symfony/asset": "^3.4",
"symfony/console": "^3.4",
"symfony/debug-pack": "*",
"symfony/expression-language": "^3.4",
"symfony/flex": "^1.0",
"symfony/form": "^3.4",
"symfony/framework-bundle": "^3.4",
"symfony/lts": "^3",
"symfony/monolog-bundle": "^3.1",
"symfony/orm-pack": "*",
"symfony/process": "^3.4",
"symfony/security-bundle": "^3.4",
"symfony/serializer-pack": "*",
"symfony/swiftmailer-bundle": "^3.1",
"symfony/translation": "^3.4",
"symfony/validator": "^3.4",
"symfony/web-link": "^3.4",
"symfony/web-server-bundle": "^3.4",
"symfony/webpack-encore-pack": "*",
"symfony/yaml": "^3.4"
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.0",
"symfony/browser-kit": "^3.4",
"symfony/css-selector": "^3.4",
"symfony/dotenv": "^3.4",
"symfony/maker-bundle": "^1.0",
"symfony/phpunit-bridge": "^3.4",
"symfony/profiler-pack": "^1.0"
},
product\u编辑。html。细枝
{% extends 'layout/base.layout.html.twig' %}
{# import relevant macros #}
{% import "macro/form_macro.html.twig" as form_macro %}
{# set translation domain #}
{% trans_default_domain 'product' %}
{% block title %}{{ "product.edit"|trans }} - {{ product.name }}{% endblock %}
{% block body %}
<!-- Product Edit -->
{{ form_start(form) }}
{{ form_macro.form_input(form.ean) }}
{{ form_macro.form_input(form.name) }}
{{ form_widget(form._token) }}
{{ form_widget(form.submit) }}
{{ form_errors(form) }}
{{ form_end() }}
<!-- Product Edit End -->
{% endblock %}
form\u宏。html。细枝
{# @param field the input field of the form #}
{% macro form_input(field) %}
<div class="input-wrapper">
{{ form_label(field) }}
{{ form_widget(field) }}
{{ form_errors(field) }}
</div>
{% endmacro %}
目前为止我所做的尝试:
-
从控制台清除缓存(php-bin/console-cache:clear)
-
我正在控制台中的内置web服务器上运行一个开发环境(php bin/控制台服务器:run)
任何帮助都将不胜感激