Bonjour,

J'ai un problème lorsque j'ajoute un champ hidden dans mon formulaire et que je le valide , j'ai l'erreur suivante : Uncaught PHP Exception Symfony\Component\PropertyAccess\Exception\InvalidArgumentException: "Expected argument of type "array", "string" given." at /srv/data/web/vhosts/a72dde2804.yatu.ws/htdocs/my_project/vendor/symfony/property-access/PropertyAccessor.php line 173

L'erreur est déclenchée par la ligne suivante : $form->handleRequest($request);

<?php
// src/Controller/RegistrationController.php
namespace App\Controller;
use App\Form\UserType_2;
use App\Entity\User_2;
use App\Events;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\Translation\TranslatorInterface;
class RegistrationController_2 extends Controller
{
    /**
     * @Route("/inscription", name="user_registration")
     * @Route("/pro/inscription", name="pro_user_registration")
     */
    public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder)
    {
        $pro = true;
        // 1) build the form
        $user = new User_2();
        $form = $this->createForm(UserType_2::class, $user);

        // 2) handle the submit (will only happen on POST)
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {

            // 3) Encode the password (you could also do this via Doctrine listener)
            $password = $passwordEncoder->encodePassword($user, $user->getPlainPassword());
            $user->setPassword($password);

            // 4) save the User!
            /*$entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($user);
            $entityManager->flush();*/

            // ... do any other work - like sending them an email, etc
            // maybe set a "flash" success message for the user

            //return $this->redirectToRoute('replace_with_some_route');
        } else {
            echo "ll";
        }

        return $this->render(
            'register.html.twig',
            array('form' => $form->createView(),"pro"=>true)
        );
    }
}
<?php
// /src/Form/UserType.php
namespace App\Form;
use App\Entity\User_2;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TelType;
use Symfony\Component\Translation\TranslatorInterface;

class UserType_2 extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $pos = strpos($_SERVER['REQUEST_URI'],"/pro/");
        if($pos === false) {
            $role = 'ROLE_USER';
        } else {        
            $role = 'ROLE_PRO';
        }

        $builder
       ->add('roles', HiddenType::class, array(
       'data' => $role,
       ))
           //->add('roles', TextType::class,array('required'   => false,'label'=>'roles','attr'=> array('class'=>'form-control')))
           ->add('firstName', TextType::class,array('required'   => false,'label'=>'firstName','attr'=> array('class'=>'form-control')))
           ->add('lastName', TextType::class,array('required'   => false,'label'=>'lastName','attr'=> array('class'=>'form-control')))
           ->add('phone', TelType::class,array('error_bubbling' => true,'label'=>'phone','attr'=> array('class'=>'form-control')))
           ->add('email', EmailType::class,array('label'=>'Email','attr'=> array('class'=>'form-control')))            
           ->add('username', TextType::class,array('label'=>'Login','attr'=> array('class'=>'form-control')))
           ->add('password', RepeatedType::class, array(
               'type' => PasswordType::class,
             'first_options'  => array('label' => 'Password','attr'=> array('class'=>'form-control')),
             'second_options' => array('label' => 'Repeat Password','attr'=> array('class'=>'form-control'))))

        ;
    }
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => User_2::class,
        ));
    }
}
?>

Je vous remercie pour votre aide

Aucune réponse