English 中文(简体)
我为什么会犯错误:“在财产道路上“名称”给出的“描述”、“损失”类型例子?
原标题:Why do I get the error: "Expected argument of type "string", "null" given at property path "name" "?

我只想简单地更新同Doctrine ORM在书状6.4中的记录。 我与一个称为“类别”的实体一起更新了这一登记册。

namespace AppEntity;
use AppRepositoryCategoriaRepository;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
use SymfonyComponentValidatorConstraintsNotNull;

#[ORMEntity(repositoryClass: CategoriaRepository::class)]
class Categoria
{
    #[ORMId]
    #[ORMGeneratedValue]
    #[ORMColumn]
    private ?int $id = null;
    #[ORMColumn(length: 100), AssertNotBlank(message:  ¡El nombre es requerido! )]
    private ?string $nombre = null;

    #[ORMColumn(length: 100), AssertNotBlank(message:  ¡El slug es requerido! )]
    private ?string $slug = null;
#Setters and getters...

我的控制者,特别是这种活动的方法和路线,都好像:

    #[Route( /doctrine/categorias/editar/{id} , name:  doctrine_categorias_editar )]
    public function categoriasEditar(int $id, Request $request, ValidatorInterface $validator, SluggerInterface $slugger): Response
    {
        $categoria = $this->em->getRepository(Categoria::class)->find($id);

        if (!$categoria) {
            throw $this->createNotFoundException( La categoría no existe. );
        }

        $form = $this->createForm(CategoriaFormType::class, $categoria);
        $form->handleRequest($request);

        var_dump($form->getData());

        if ($form->isSubmitted() && $form->isValid()) {
            var_dump($form->getData());
            // $this->em->flush();
            // return $this->redirectToRoute( categoria_inicio );
        } else {
            //throw $this->createAccessDeniedException("Valores erroneos!");
        }

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

形式如下:

class CategoriaFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
#I use [ required  => false] for test errors
            ->add( nombre , TextType::class, [ required  => false])
            ->add( slug , TextType::class, [ required  => false])
            ->add( enviar , SubmitType::class)
        ;
    }

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

The problem is that when I click the "enviar" button, leaving the "nombre" field empty, I would expect the Asserts to do their job and redirect me or something. But I immediately get the error: Expected argument of type "string", "null" given at property path "name". Now, traditionally I also tried:

                $errors = $validator->validate($categoria);

                if (count($errors) > 0) {
                    return $this->render( doctrine/categorias_editar.html.twig , [ form  => $form,  errors  => $errors]);
                } else {

                    $campos = $form->getData();
    
                    $categoria->setNombre($campos->getNombre());
                    $categoria->setSlug($campos->getSlug());
    
                    //$this->em->persist($categoria);
    
                    $this->em->flush();
                    
                    return $this->redirectToRoute( doctrine_inicio );
                }

Where do I ask, if I had errors. In the same way, I would expect it to get here and just that, it would tell me that there are errors, but nothing, it just skips the message already mentioned.

Another curiosity is in the "IF" "if ($form->isSubmitted() && $form->isValid())" from the controller, If I put a var_dump() or a die( ) ​​before, it executes it correctly, if it is inside the if, it never goes through there. only the error occurs.

What is happening? What am I doing wrong?

我曾尝试过两种办法,以“isValid(>)”的方式来弥补这些错误,并在“$erors =$validator->”;validate($categoria);,如果存在错误(现值($erors) >0),甚至发生差错,跨国计算机应用系统(var_dumps <>/code>或

if ($form->isSubmitted() && $form->isValid()) {
```"
问题回答

This is funny! I only have to add a "?" in my Entity at the setter method!. Like this:

public function setNombre(?string $nombre): static
{
    $this->nombre = $nombre;

    return $this;
}
public function setSlug(?string $slug): static
{
    $this->slug = $slug;

    return $this;
}

Now validators works!. Still, thanks everyone for looking at my question! But I still don t understand why it still doesn t execute a var_dump or a die() inside the if with isSubmitted()





相关问题
Symfony 5, I can t start the server

I develop a symfony project and everything was fine. But since yesterday I can not launch the server: enter image description here The commands symfony server:start and symfony server:stop don t work ...

learning symfony 1.4 will be good when using symfony 2.0?

I know that the architecture is different in symfony 2.0 but im learning 1.4 right now. I wonder if this knowledge i gain about 1.4 will be usable for 2.0 in some extent or will it be a total waste ...

Is symfony 2.0 stable enough to use? [closed]

I wonder if Symfony 2.0 is stable enough to use? Because I ve never used Symfony before. It seems that Symfony 2 is much better than the previous version and I don t want to relearn/recode ...

your experience using symfony 2.0 [closed]

I m going to start a new project that s building web apps from scratch. I have been thinking about using symfony framework for this project. Should I start using symfony 2.0 or stick with 1.4 ? I ...

How to increase the session timeout in Symfony

I would like to know how to increase the session timeout in symfony. Is it enough to only adjust the symfony configuration settings or must I also configure anything in my php.ini file?

How stable or unstable is symfony 2.0? [closed]

Well, I know it s a preview, and I know it says that it s not yet ready for production, and yet I dare ask the question. I need to start building a pretty big application, which is planned to go live ...

热门标签