Here is the form class incase thats doing something?

问题描述 投票:0回答:1

<?php

namespace App\Entity\Contact;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Table(name="contact_contact")
 */
class Contact
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer", options={"unsigned":true})
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @Assert\NotBlank
     * @ORM\Column(type="string", length=40, nullable=true)
     */
    private $fname;

    /**
     * @ORM\Column(type="string", length=40, nullable=true)
     */
    private $lname;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getFname(): ?string
    {
        return $this->fname;
    }

    public function setFname(string $fname): self
    {
        $this->fname = $fname;

        return $this;
    }

    public function getLname(): ?string
    {
        return $this->lname;
    }

    public function setLname(?string $lname): self
    {
        $this->lname = $lname;

        return $this;
    }

}

Here is my entity:

/**
 * @Route("/{id}/edit", name="contact_contact_edit", methods={"GET","POST"})
 */
public function edit(Request $request, Contact $contact): Response
{
    $form = $this->createForm(ContactType::class, $contact);
    $form->handleRequest($request);

    if ($form->isSubmitted()) {
            if ($form->isValid()) {
                $this->getDoctrine()->getManager()->flush();
            }
    }

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

That's one of the reasons you should avoid allowing the form component changing your entities directly. It will set the data, and then validate it. So it's totally possible for the entity to be in an invalid state after the form has been submitted.Anyway, you can specify what an empty value should be:

这是我的实体。

这是编辑控制器的动作代码。

当我发布表单时,将fname(名字)字段留空... ...我得到了这个错误(

Symfony/Component/PropertyAccess/Exception/InvalidArgumentException

)

<?php

namespace App\Form\Contact;

use App\Entity\Contact\Contact;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ContactType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('fname', TextType::class, ['label' => 'First Name'])
            ->add('lname', TextType::class, ['label' => 'Last Name']);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Contact::class,
            'min_entry' => false,

            // NOTE: Must be added to every form class to disable HTML5 validation
            'attr' => ['novalidate' => 'novalidate']
        ]);

        $resolver->setAllowedTypes('min_entry', 'bool');
    }
}
symfony symfony4 symfony-forms
1个回答
0
投票

我错过了什么?

EDIT

© www.soinside.com 2019 - 2024. All rights reserved.