0

呈现表单时,如何仅为集合中的第一项设置只读选项?

我的简单模型:

class Main
{
    public $others;
}

class Other
{
    public $field1;

    public $field2;
}

我的模型的简单表单类型:

class MainType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('others', 'collection', array(
                'type' => new OtherType(),
                'allow_delete' => true,
                'allow_add' => true,
                'by_reference' => false,
            ))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'App\MyBundle\Entity\Main',
        ));
    }

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

class OtherType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('field1')
            ->add('field2')
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'App\MyBundle\Entity\Other',
        ));
    }

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

和简单的操作方法我的控制器

//...
public function indexAction($id)
{
    $main = new Main();

            $other1 = new Other();
    $other1->field1 = 'a';
    $other1->field2 = 'b';
    $main->others[] = $other;

            $other2 = new Other();
    $other2->field1 = 'c';
    $other2->field1 = 'd';
    $main->others[] = $other;

    $form = $this->createForm(new MainType(), $main);

    //...isValid, persist, flush...
}
//...

我可以在手动呈现表单时设置条件,但我想知道是否可以在表单代码处输入这样的限制。

4

1 回答 1

2

目前不可能让集合的行有不同的选项。如果您认为这将是一个有价值的补充,我邀请您在问题跟踪器上创建一个功能请求。

于 2012-07-31T16:57:03.970 回答