类型"字符串或Symfony \ Component \ Form \ FormTypeInterface"的预期参数;

时间:2015-06-09 02:41:46

标签: php forms symfony

symfony中出现的错误如下:

类型"字符串或Symfony \ Component \ Form \ FormTypeInterface"," Proyecto \ LavocBundle \ Entity \ Datos"的预期参数给定

我不会把所有代码都放在一起,以免它变得太大,我认为问题出在Type:

DatosType:

    <?php

namespace Proyecto\LavocBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class DatosType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) 
    {
        $builder->add('empresa');
        $builder->add('cuit');
        $builder->add('localidad');
        $builder->add('calle');
        $builder->add('altura');
        $builder->add('areaTel');
        $builder->add('telefono');
        $builder->add('areaCel');
        $builder->add('celular');
        $builder->add('email');
    }

    public function getName()
    {
    return 'datos_form';
    }

}

的用户类型:

    <?php

namespace Proyecto\LavocBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class UserType extends AbstractType 
{
    public function buildForm(FormBuilderInterface $builder, array $options) 
    {
        $builder->add('usuario');
        $builder->add('pass');
    }

    public function getName() 
    {
        return 'user_form';
    }
}

RegistroUsuarioType(这是嵌入式表单):

<?php

namespace Proyecto\LavocBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Proyecto\LavocBundle\Entity\Datos;



class RegistroUsuarioType extends AbstractType 
{
    public function buildForm(FormBuilderInterface $builder, array $options) {
        parent::buildForm($builder, $options);

        $builder->add('usuario', 'text');
        $builder->add('pass', 'text');
        $builder->add('datos', new Datos());
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Proyecto\LavocBundle\Entity\User'
        ));
    }

    public function getName() {
        return 'RegistroUsuario_form';
    }
}

TablaController:

public function crearUsuarioDatosAction()
    {
        $request = $this->getRequest();
        $usuario = new User();
        $datos = new Datos();
        $usuario->setDatos($datos);

        $form = $this->createForm(new RegistroUsuarioType(), $usuario);


        if($request->getMethod() == 'POST')
            {
                        $form->bind($request);


                if( $form->isValid() )
                    {
                        $em = $this->getDoctrine()->getEntityManager();
                        $em->persist($form);
                        $em->flush();

                    return $this->redirect($this->generateUrl('home'));

                    }



            }

            return $this->render('AtajoBundle:IngresarValores:crearUsuarioDatos.html.twig', array('form' => $form->createView()));

    }

非常感谢你的帮助

1 个答案:

答案 0 :(得分:1)

    $builder->add('datos', new Datos());

应该是

    $builder->add('datos', new DatosType());

此外,您的DatosType需要data_class属性集,将此函数添加到您的DatosType类:

public function setDefaultOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Proyecto\LavocBundle\Entity\Datos',
    ));
}

对于较新版本的Symfony2 configureOptions而不是setDefaultOptions

确保您的DatosType具有:

use Symfony\Component\OptionsResolver\OptionsResolverInterface;