Symfony表单无法呈现

时间:2018-02-18 12:29:28

标签: forms symfony symfony4

一个简单的表单不呈现,它会抛出一个错误:

  

类型错误:参数1传递给   Symfony \ Component \ Form \ FormRenderer :: renderBlock()必须是一个实例   Symfony \ Component \ Form \ FormView,没有给出,调用   C:\开发\项目\ ShopStock \ VAR \缓存\ dev的\树枝\ 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"
        ));
    }
}

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)

我在控制台中运行Web服务器中的开发环境(php bin / console server:run)

非常感谢任何帮助

1 个答案:

答案 0 :(得分:1)

product_edit.html.twig 中,您忘记在 form_end 中提供表单。变化:

{{ form_end(form) }}