Reputation: 51
The error here is not explicit, from what I think it is related just to a Type difference between parameters but i have not found the way to resolve this...
The EventListener
is receiving 2 parameters but it doesn't provide the information of which of them is the "1" argument with the issue but I think it is the 2nd due to the error says an instance of UserBundle\Form\FormEvent on singular and the first argument is a generic event from the FormEvents
class which is on plural.
Error Shown:
Type error: Argument 1 passed to UserBundle\Form\UserType::UserBundle\Form{closure}() must be an instance of UserBundle\Form\FormEvent, instance of Symfony\Component\Form\FormEvent given
<?php
namespace UserBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\FormEvents;
class UserType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username',HiddenType::class)
->add('firstname',TextType::class)
->add('lastname',TextType::class)
->add('email', RepeatedType::class,
array('type' => EmailType::class, 'invalid_message' => 'The email fields must match.','options'
=> array('attr' => array('class' => 'email-field')),'required' => true, 'first_options'
=> array('label' => 'Email'),'second_options' => array('label' => 'Confirm email'),))
->add('password', RepeatedType::class,
array('type' => PasswordType::class, 'invalid_message' => 'The password fields must match.','options'
=> array('attr' => array('class' => 'password-field')),'required' => true, 'first_options'
=> array('label' => 'Password'),'second_options' => array('label' => 'Confirm password'),))
->add('save',SubmitType::class)
->addEventListener(FormEvents::PRE_SUBMIT, function(FormEvent $event) {
$data = $event->getData();
$form = $event->getForm();
if ($data['firstname'] !== null && $data['lastname'] !== null){
$username = $data['firstname']." ".$data['lastname'];
$data['username'] = $username;
$event->setData($data);
}
});
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'UserBundle\Entity\User'
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'user';
}
}
I have followed the steps given by symfony documentation
Upvotes: 1
Views: 1332
Reputation: 2596
You're missing the FormEvent
class import.
use Symfony\Component\Form\FormEvent;
Since you did not import it, it used the current namespace for the class (UserBundle\Form\FormEvent
). But this class doesn't exist.
Upvotes: 5