Symfony 表单不呈现
Symfony Form not rendering
一个简单的表单没有呈现,它抛出一个错误:
Type error: Argument 1 passed to
Symfony\Component\Form\FormRenderer::renderBlock() must be an instance
of Symfony\Component\Form\FormView, none given, called in
C:\Development\Projects\ShopStock\var\cache\dev\twig4a2b61bf8b4b2e2549f7da9c9acb553cbf793088729905b2adf9cd03dbe799.php
on line 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"
));
}
}
ProductController.php:
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;
}
}
composer.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_edit.html.twig
{% 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_macro.html.twig
{# @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)
- 正在删除缓存目录({project-root-dir}/var/cache)
我是 运行 一个开发环境,来自控制台中内置的网络服务器 (php bin/console server:run)
任何帮助将不胜感激
在您的product_edit.html.twig中,您忘记在form_end中提供表格 。变化:
{{ form_end(form) }}
一个简单的表单没有呈现,它抛出一个错误:
Type error: Argument 1 passed to Symfony\Component\Form\FormRenderer::renderBlock() must be an instance of Symfony\Component\Form\FormView, none given, called in C:\Development\Projects\ShopStock\var\cache\dev\twig4a2b61bf8b4b2e2549f7da9c9acb553cbf793088729905b2adf9cd03dbe799.php on line 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"
));
}
}
ProductController.php:
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;
}
}
composer.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_edit.html.twig
{% 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_macro.html.twig
{# @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)
- 正在删除缓存目录({project-root-dir}/var/cache)
我是 运行 一个开发环境,来自控制台中内置的网络服务器 (php bin/console server:run)
任何帮助将不胜感激
在您的product_edit.html.twig中,您忘记在form_end中提供表格 。变化:
{{ form_end(form) }}