带有选择字段的Symfony表单生成器导致数组到字符串转换错误

eaf3rand  于 2023-05-18  发布在  其他
关注(0)|答案(1)|浏览(166)

Symfony 6
教义3
我使用make:formmake:crud命令自动生成所有内容,所以我不知道为什么这会导致错误。我理解错误;因此它是不言自明的。但我不知道在哪里和如何修复它。

我的控制器:

#[Route('/{isbn}/edit', name: 'app_isbn_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Isbn $isbn, EntityManagerInterface $entityManager): Response
{
    $form = $this->createForm(IsbnType::class, $isbn);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $entityManager->flush();

        return $this->redirectToRoute('app_isbn_index', [], Response::HTTP_SEE_OTHER);
    }

    return $this->renderForm('isbn/edit.html.twig', [
        'isbn' => $isbn,
        'form' => $form,
    ]);
}

我的实体**

<?php

namespace App\Entity;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

/**
 * Isbn
 *
 * @ORM\Table(name="isbn", uniqueConstraints={@ORM\UniqueConstraint(name="isbn", columns={"isbn"})})
 * @ORM\Entity
 */
class Isbn
{
    /**
     * @var int
     *
     * @ORM\Column(name="isbn", type="bigint", nullable=false, options={"unsigned"=true})
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $isbn;

    /**
     * @var array
     *
     * @ORM\Column(name="state", type="simple_array", length=0, nullable=false, options={"default"="Free"})
     */
    private $state = 'Free';

    public function getIsbn(): ?string
    {
        return $this->isbn;
    }

    public function getState(): array
    {
        return $this->state;
    }

    public function setState(array $state): self
    {
        $this->state = $state;

        return $this;
    }

}

表格

<?php

namespace App\Form;

use App\Entity\Isbn;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class IsbnType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('state', ChoiceType::class, [
                'choices' => [
                    'Free' => 'Free',
                    'Used' => 'Used',
                    'Dead' => 'Dead'
                ],
                'label' => 'State :',
                'expanded' => false,
                'multiple' => false
            ]);
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => Isbn::class,
        ]);
    }
}

edit.html.twig

{% extends 'base.html.twig' %}

{% block title %}Edit Isbn{% endblock %}

{% block body %}
    <h1>Edit Isbn</h1>

    {{ include('isbn/_form.html.twig', {'button_label': 'Update'}) }}

    <a href="{{ path('app_isbn_index') }}">back to list</a>

    {{ include('isbn/_delete_form.html.twig') }}
{% endblock %}

_form.html.twig

{{ form_start(form) }}
    {{ form_widget(form) }}
    <button class="btn">{{ button_label|default('Save') }}</button>
{{ form_end(form) }}

错误

警告:数组到字符串转换

sqxo8psd

sqxo8psd1#

我不认为它来自你的FormType,在你的实体中,你混合了字符串和数组类型提示,为你的state属性。
检查:

/**
     * @var array
     *
     * @ORM\Column(name="state", type="simple_array", length=0, nullable=false, options={"default"="Free"})
     */
    private $state = 'Free';

您正在影响默认值Free,它是state属性的字符串。我认为你应该将属性和getter/setter更改为:

private string $state = 'Free';

    public function getState(): string
    {
        return $this->state;
    }

    public function setState(string $state): self
    {
        $this->state = $state;

        return $this;
    }

相关问题