Skip to content
Advertisement

Can’t register an entity in my database with form symfony

I’m new with symfony and I try to register an entity in my base with form. Here is the form:

<?php
namespace AppForm;

use SymfonyComponentFormAbstractType;
use SymfonyComponentFormExtensionCoreTypeMoneyType;
use SymfonyComponentFormExtensionCoreTypeTelType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentFormExtensionCoreTypeSubmitType;
use SymfonyComponentFormExtensionCoreTypeTextType;
use SymfonyComponentFormExtensionCoreTypeNumberType;

class OfferType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('title', TextType::class, array('label' => '*Title: ' , 'attr' => array('class' => 'input2')))
        ->add('nameofgame', TextType::class, array('label' => 'Name of the Game:','attr' => array('class' => 'create button1')))
        ->add('description', TextType::class, array('label' => 'Description: ' , 'attr' => array('class' => 'input2')))
        ->add('price', MoneyType::class, array('label' => '*Price: ' , 'attr' => array('class' => 'input2')))
        ->add('numbertelephone', TelType::class, array('label' => 'Telephone Number:','attr' => array('class' => 'create button1')))
        ->add('save', SubmitType::class, array('label' => 'Post Offer','attr' => array('class' => 'create button1')))
    ;
}
}

Here is my controller:

/**
 * @Route("/BoardFind/Trade/RegisterOffer", name="RegisterOffer")
 * @param Request $request
 * @return SymfonyComponentHttpFoundationRedirectResponse|SymfonyComponentHttpFoundationResponse
 */
public function register(Request $request)
{
    $Offer = new TradeOffer();
    $form = $this->createForm(OfferType::class, $Offer);
    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
        $Offer = $form->getData();
        echo maleee;
        $entityManager = $this->getDoctrine()->getManager();

        $session = $this->get('session');
        $userId = $session->get("id");
        $user = $this->getDoctrine()
            ->getRepository(User::class)->find($userId);
        $Offer->setUser($user);
        $Offer->setTraderName($user->getName());
        $Offer->setTraderLastName($user->getLastName());
        $Offer->setUsername($user->getUsername());
        $Offer->setTraderEmail($user->getEmail());

        $entityManager->persist($Offer);
        $entityManager->flush();

        $flashbag = $this->get('session')->getFlashBag();
        $flashbag->add("SuccessfullRegister", "You successfully registered your offer!");
        return $this->redirectToRoute('Trade');
    }

    return $this->render('home/RegisterOffer.html.twig', array(
        'form' => $form->createView(),
    ));

}

When I try to register an offer it says that “Expected argument of type “AppEntitydouble”, “double” given.” but the thing is that I think the property of the entity is right. Here is the property:

/**
 * @AssertType("double")
 * @AssertNotBlank()
 */
private $price;

If you want image of the error here it is. So where is the problem?

Advertisement

Answer

In PHP double is an alias for float. When using scalar type hints or return types this alias is not supported and the actual type must be used instead. This behavior is comparable with bool being supported, but not boolean.

In other words in your getters & setters you have to refer to to float instead.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement