Symfony 2使用表单时出错

时间:2015-11-06 14:36:56

标签: php forms symfony doctrine twig

我遇到了symfony 2和表单

的大问题

我已经按照这条指示......

http://symfony.com/doc/current/cookbook/form/form_collections.html

第一节课(如TAG):

public string cn{
    get {return courseNumber; }
    set {if (value != null)
        courseNumber = value;
    }
}
public string name{
    get { return courseName; }
    set
    {
        if (value != "")
            courseName = value;
    }
}
public int hours{
    get {return courseHours; }
    set {if (value != 0)
        courseHours = value;
    }
}

然后我编写了容器类(TASK):

class TipoPermessoFerie {
    public $id;
    public $nome;
    public $descrizione;
    public $permesso;
}

和2个包装类(TAGTYPE和TASKTYPE):

class Form1 {
    protected $descrizione;
    protected $tipoPermessoFerie; // ArrayCollection of TipoPermessoFerie

    // metodi

    public function __construct(){
        $this->tipoPermessoFerie = new ArrayCollection();
    }

    public function getDescrizione () {
        return $this->descrizione;
    }

    public function setDescrizione ($de) {
        $this->descrizione = $de;
    }

    // get un ArrayCollection
    public function getTipoPermessoFerie () {
        return $this->tipoPermessoFerie;
    }

}

控制器的工作原理如下

class TipoPermessoFerieType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder->add('nome');
        $builder->add('descrizione');
        $builder->add('permesso');
        $builder->add('id');
    }

    public function configureOptions(OptionsResolver $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\TipoPermessoFerie',
        ));
    }

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

class Form1Type extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder->add('descrizione');
        $builder->add('tipoPermessoFerie', 'collection', array('type' => new TipoPermessoFerieType()));
        $builder->setMethod('GET');
        $builder->add('save', 'submit', array('label' => '< Fase 2 >'));
        $builder->add('reset', 'reset', array('label' => '< Annulla >'));
    }

    public function configureOptions(OptionsResolver $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Form1',
        ));
    }

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

********此时我发现错误:

  

表单的视图数据应该是标量,数组或类型   \ ArrayAccess的实例,但是类
的实例   的appbundle \实体\ TipoPermessoFerie。你可以通过
避免这个错误   设置&#34; data_class&#34;选项到   &#34;的appbundle \实体\ TipoPermessoFerie&#34;或者通过添加视图变压器
  转换类的实例   AppBundle \ Entity \ TipoPermessoFerie标量,数组或实例   \ ArrayAccess接口。

$repos = $this->getDoctrine()->getRepository('AppBundle:TipoPermessoFerie');
    $tipiF = $repos->findBy(array('permesso' => 'F')); // many
    ...
    dump($tipiF); //this is ok  
    $f1 = New Form1 ();
    $f1->setDescrizione("Fase 1");
    for ($i = 0; $i < count($tipiF); $i++) {
        dump($tipiF[$i]); //OK
        $f1->getTipoPermessoFerie()->add($tipiF[$i]);
    }

    dump($f1); //f1 is istance of Form1
    $form = $this->createForm(new Form1Type(), $f1);

我不知道出了什么问题......

有人可以帮助我吗?

0 个答案:

没有答案