0

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.

1 Answer 1

2
  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()) {
            // ...
        }
    }
    
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer, what do you mean by name attributes? I was trying to figure it out but I couldn't. I also changed the $product variable to the one you specified, but I still get the same error saying that it was expecting array or object and received string.
<input type="text" id="pName" placeholder="Product name"> - the attributes in this element are: type, id, and placeholder. A form field, such as input with type text, needs a name attribute, under which the value will be sent to the server with a POST request. For example, a value with this field: <input type="text" name="theFieldName"> will be later accessible in $request object like this: $request->request->get('theFieldName'), and will also be present in $_POST['theFieldName'].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.