Hello,
I’m new to symfony. I’m trying to create a simple contact form, but I’ve got an error message :
Uncaught PHP Exception Symfony\Component\PropertyAccess\Exception\InvalidArgumentException: “Expected argument of type “EmailType”, “string” given” at /mnt/400Go/www/sy1/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php line 255
I understand the message (the email field of my form resolves into a string), but I can’t see where to look in my code. If someone can have a look to my files, it would be great.
Best regards,
MC
Here is the entity :
// src/AppBundle/Entity/ContactSetClass.php
namespace AppBundle\Entity;
/* */
class ContactSetClass
{
protected $contactName;
protected $contactEmail;
public function getContactName()
{
return $this->contactName;
}
public function setContactName($contactName)
{
$this->contactName = $contactName;
}
public function getContactEmail()
{
return $this->contactEmail;
}
public function setContactEmail(\EmailType $contactEmail = null)
{
$this->contactEmail = $contactEmail;
}
/**
* @var integer
*/
private $id;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
}
Here is the type :
/* AppBundle/Form/Type/ContactsFormType.php */
namespace AppBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\OptionsResolver\OptionsResolver;
//use Doctrine\ORM\EntityManager;
/* */
class ContactsFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->setMethod('POST')
->add('contactName')
->add('contactEmail', EmailType::class, array('attr' => array('placeholder' => 'Your email address')))
->add('save', SubmitType::class, array('label' => 'Add contact'))
;
}
public function getName()
{
return 'contacts_form';
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class'=>'AppBundle\Entity\ContactSetClass',
));
}
}
Here is the controller :
// src/AppBundle/Controller/ContactsFormController.php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use AppBundle\Entity\ContactSetClass;
use AppBundle\Form\Type\ContactsFormType;
class ContactsFormController extends Controller
{
public function contactsFormAction(Request $request)
{
$contactSet=new ContactSetClass();
$ContactsFormType=new ContactsFormType();
$form=$this->createForm(ContactsFormType::class, $contactSet);
$form->handleRequest($request);
if($form->isSubmitted())
{
if($form->isValid())
{
$contactName=$form->get('contactName')->getData();
$contactEmail=$form->get('contactEmail')->getData();
$contactSet->setContactName($contactName);
$contactSet->setContactEmail($contactEmail);
$entityManager=$this->getDoctrine()->getManager();
$entityManager->persist($contactSet);
$entityManager->flush();
return new Response('Contact '.$contactName.' has been created for '.$contactEmail.'.');
}
else
{
$message='Please correct your data.';
}
}
else
{
$message='Not submitted ';
}
return $this->render('contactsForm.html.twig', array('form' => $form->createView(),'message'=>$message,));
}
}
Here is the ORM file :
# src/AppBundle/Resources/config/doctrine/ContactSetClass.orm.yml
AppBundle\Entity\ContactSetClass:
type: entity
table: contacts
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
contactName:
type: string
length: 100
contactEmail:
type: string
length: 100
Here is the validation file :
AppBundle\Entity\ContactSetClass:
properties:
contactName:
- NotBlank: {message: "Please provide a contact name."}
- Type: string
contactEmail:
- Email:
message: The email "{{ value }}" is not a valid email address.
checkMX: true
Here is the template :
{# app/Resources/views/default/contactsForm.html.twig #}
{% block contactsForm %}
<p style="color: Red;">{{ message }}</p>
{{ form_start(form, {'attr': {'novalidate': 'novalidate'}}) }}
{{ form_widget(form) }}
{{ form_rest(form) }}
{{ form_end(form) }}
{% endblock %}