Warning: Attempt to read property "type" on null : symfony

432 views Asked by At

I am new to symfony and i've got this error "Warning: Attempt to read property "type" on null" i don't know how to fix it i

've tried clearing the cache checking the typoes 

none of this fixed it here is my controller code

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManagerInterface;
use App\Repository\UsersRepository;
use App\Entity\Users;
use App\Form\UserType;
use Symfony\Component\Intl\Countries;

class UserFormController extends AbstractController
{
    private $entityManager;

    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    private function getCountries()
    {
        return array_flip(Countries::getNames()); // Use Countries class to get country names
    }

    #[Route('/user', name: 'form_index')]
    public function index(UsersRepository $userRepository): Response
    {
        $users = $userRepository->findAll(); // Renamed $user to $users to represent a collection

        return $this->render('user_form/index.html.twig', [
            'users' => $users, // Updated key name to be more descriptive
        ]);
    }

    #[Route('/user/create', name: 'user_create')]
    public function create(Request $request): Response
    {
        $user = new Users();

        $form = $this->createForm(UserType::class, $user, [
            'country' => $this->getCountries(),
        ]);

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

    #[Route('/user/store', name: 'user_store')]
    public function store(Request $request): Response
    {
        $user = new Users();

        $form = $this->createForm(UserType::class, $user, [
            'country' => $this->getCountries(),
        ]);

        if ($form->isSubmitted() && $form->isValid()) { // Removed redundant request method check
            $this->entityManager->persist($user);
            $this->entityManager->flush();

            return $this->redirectToRoute('form_success');
        }

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

    #[Route('/user/edit/{id}', name: 'form_edit')]
    public function edit(Request $request, int $id, UsersRepository $userRepository): Response
    {
        $user = $userRepository->find($id);

        if (!$user) {
            throw $this->createNotFoundException('User not found');
        }

        $form = $this->createForm(UserType::class, $user);

        $form->handleRequest($request);

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

            return $this->redirectToRoute('form_success');
        }

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

    #[Route('/user/delete/{id}', name: 'form_delete')]
    public function delete(int $id, UsersRepository $userRepository, EntityManagerInterface $entityManager): Response
    {
        $user = $userRepository->find($id);

type here


        if (!$user) {
            throw $this->createNotFoundException('User not found');
        }

        $entityManager->remove($user);
        $entityManager->flush();

        return $this->redirectToRoute('form_success');
    }

    #[Route('/user', name: 'form_success')]
    public function success(): Response
    {
        return $this->render('user_form/index.html.twig');
    }
}

type her<?php

namespace App\Form;

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

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('name', null, [
                'label' => 'Prénom',
            ])
            ->add('surname', null, [
                'label' => 'Nom',
            ])
            ->add('dateOfBirth', DateType::class, [
                'widget' => 'single_text',
                'html5' => true,
                'format' => 'yyyy-MM-dd',
                'label' => 'Date de naissance',
            ])
            ->add('role', null, [
                'label' => 'Role',
            ])
            ->add('country', ChoiceType::class, [
                'choices' => $options['country'],
                'label' => 'Pays',
            ])
            ->add('region', null, [
                'label' => 'Région',
            ]);
    }

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

create.html.twig


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
        <h1>Create User</h1>
    <form method="POST" action="{{ path('user_store') }}">

        {{ form_widget(form) }}
        <button type="submit" class="btn btn-primary">Creation</button>
    </form>
</body>
</html>

i want to know the reason behing this mulfunction and how to fix it .I've tried clearing the cache and checking the typoes

1

There are 1 answers

2
eremalauda On

i fixed my bug the error occured when i tried to work with the @assert to validate my data there was a syntax error here is the corrected format

`/**

  • @ORM\Column(type="string", nullable=false)
  • @Assert\NotBlank
  • @Assert\Regex(
  • pattern="/^[a-zA-Z]{2,10}$/",
    
  • message="The name must be between 2 and 10 characters and contain only letters."
    
  • ) */`