elanon
elanon

Reputation: 23

symfony2 collection form, save to entity

I'm new to Symfony. I want to do collection of forms. I'm getging something like:

class SkillType extends AbstractType
public function getName()
{
    return 'skills_cfg';
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', 'text', array(
        'label' = 'name'

    ))
        ->add('name_short', 'text', array(
            'label' => 'short name'
        ));
}

}

And I get a Form where I want to use collection on that form is before:

class AbsType extends AbstractType

{
private $object;

public function __construct($object)
{
    $this->object = $object;
}

public function getName()
{
    return '';
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('row', 'collection', array(
            'type' => $this->object,
            'allow_add' => true,
            'prototype' => true,
        ))
        ->add('addMore', 'button', array(
            'attr' => array(
                'value' => 'add more'
            ),
        ))
        ->add('submit', 'submit', array(
            'attr' => array(
                'value'=>'save',
            ),
            'label' => 'save'
        ));
}

}

And in my controller i getting forms like that:

class InstallController extends Controller

{

public function indexAction($configPage, Request $request)
{

    $skillForm= $this->createForm(new AbsType(new SkillType()));
    $SkillConfig=new SkillsCfg();
    if($request->isMethod('POST')) {
        $skillForm->handleRequest($request);
            if ($skillForm->isValid()) {
            $em=$this->getDoctrine()->getManager();
            $em->persist($SkillConfig);
            $em->flush();
        }
    }
    return array(
        'form' => $skillForm->createView(),
        'page' => $configPage
    );
}

The entity and view looks like that:

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=30)
 */
private $name;

/**
 * @var string
 *
 * @ORM\Column(name="name_short", type="string", length=10)
 */
private $nameShort;

Of course i get getters and setters.

View:

var emailCount = '{{ form.row|length }}';

jQuery(document).ready(function() {
jQuery('#addMore').click(function(e) {
e.preventDefault();

var emailList = jQuery('#fields-list');

// grab the prototype template
var newWidget = emailList.attr('data-prototype');
// replace the "__name__" used in the id and name of the prototype
// with a number that's unique to your emails
// end name attribute looks like name="contact[emails][2]"
newWidget = newWidget.replace(/__name__/g, emailCount);
emailCount++;

// create a new list element and add it to the list
var newLi = jQuery('<li></li>').html(newWidget);
newLi.appendTo(emailList);
});
})
    </script>
{% endblock javascripts %}
{% block body %}
    {{ form_start(form) }}
    <ul id="fields-list"
    data-prototype="{{ form_widget(form.row.vars.prototype)|e }}">
        {% for newRow in form.row %}
            <li>
                {{ form_errors(newRow) }}
                {{ form_widget(newRow) }}
            </li>
        {% endfor %}
    </ul>
    {{ form_end(form) }}
{% endblock body %}

And I get an error:

An exception occurred while executing 'INSERT INTO skills_cfg (name, name_short) VALUES (?, ?)' with params [null, null]:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null

Why does it happen? And how do I solve it?

Upvotes: 0

Views: 484

Answers (1)

Frank B
Frank B

Reputation: 3697

Change the indexAction like this:

public function indexAction($configPage, Request $request)
{
    $em=$this->getDoctrine()->getManager();

    $skillForm= $this->createForm(new AbsType(new SkillsCfg()));

    $skillForm->handleRequest($request);

    if ($skillForm->isValid())
    {
        foreach($form->get('row')->getData() as $row)
        {
            $em->persist($row);
        }
        $em->flush();
    }

    return array(
        'form' => $skillForm->createView(),
        'page' => $configPage
    );
}

If its still not working start debugging. Try to get the data from row and check if this is an array.

Upvotes: 1

Related Questions