2-D
2-D

Reputation: 23

Symfony3 trying to save into db with forms and twig

It is my first attempt to store product's information into mysql. I apologize in advance if the code is not in the best form, Im still learning. This is my registration.html.twig

{% extends 'base.html.twig' %}

    {% block stylesheets %}
        <link href="{{ asset('bundles/framework/css/myStyle.css') }}" rel="stylesheet" />
    {% endblock %}

{% block title %}Create your product {% endblock %}


{% block body %}
    <div id="container">
    <h1 id="header">Create your product and store it in the database!</h1>
    </div>

    <form method="POST" id="registrationForm">
    <input type="text" id="pName" placeholder="Product name">
        <input type="text" id="pPrice" placeholder="Product price"> <br>
        <textarea id="pDescription" placeholder="Product description"></textarea>
    </form>
    <input type="submit" value="Submit" >

{% endblock %}

I also have Product entity class with setters&getters, not going to include it here because of amount of code.

This is my ProductForm :

class ProductForm extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
       $builder
           ->add('pName')
           ->add('pPrice')
           ->add('pDescription')
       ;
    }
}

and here is part of my controller for the /insert page:

public function createAction()
    {
        $product = 'AppBundle\Entity\Product';
        $form = $this->createForm(ProductForm::class, $product);

        if($form->isValid())
        {
            $em= $this->getDoctrine()->getManager();
            //Save into database code should go here...
        }
    }

For starters, it complains that it Expected argument of type "object, array or empty", "string" given which I guess comes from the controller where I try to pass $product variable with specified path to the entity class. In the documentation this part is very confusing. It says $product = ... leaving me without anything, I managed to understand that this is a path to entity class which should be passed to createForm method, but as mentioned before it complains that it is string, not array. Could someone review my code and give a feedback on what is wrong? Im really lost at the moment and not sure what to do next or how to solve this. Thank you in advance.

Upvotes: 0

Views: 387

Answers (1)

Bartosz Zasada
Bartosz Zasada

Reputation: 3900

  1. Your form fields don't have name attributes set, which means that no data will be posted from it.

  2. In the controller, $product should be an instance of AppBundle\Entity\Product, not just a class name:

    $product = new \AppBundle\Entity\Product();
    
  3. The form will not automatically retrieve the data from HTTP request (which are not there because of point 1), you must handle the request manually:

    // inject the request here
    public function createAction(\Symfony\Component\HttpFoundation\Request $request)        {
        // ... 
        $form->handleRequest($request);
        if ($form->isValid()) {
            // ...
        }
    }
    

Upvotes: 2

Related Questions