【问题标题】:What to do when form choice field depends on value of domain object property当表单选择字段取决于域对象属性的值时该怎么办
【发布时间】:2011-09-24 08:34:15
【问题描述】:

当表单选择字段依赖于 域对象。我有应包含保险的保险字段 特定用户。

http://pastie.org/2132730

提前致谢

【问题讨论】:

    标签: symfony


    【解决方案1】:

    如果保险选择是用户的已知值,那么您可以在创建表单时将它们作为选项传递:

    $form = $this->createForm(new AgentContractFormType(), $agentContract, array(
        'insurances' => array(/* insurance choices here */),
    ));
    

    然后在你的表单类中:

    public function getDefaultOptions(array $options)
    {
        return array(
            'insurances' => $options['insurances'],
            'data_class' => 'NTO\DocumentBundle\Entity\Document\AgentContract',
        );
    }
    

    然后您可以随意在buildForm() 中使用它们。希望对您有所帮助。

    【讨论】:

    • 或者也可以通过 DI 传递 smth 槽以形成类型,但你的方法是我最终采用的方法(只是代替选项中的 'insurances' 'repository' + 'userId')
    猜你喜欢
    • 1970-01-01
    • 2012-05-23
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    • 2021-02-01
    • 2016-11-02
    • 2014-03-23
    • 1970-01-01
    相关资源
    最近更新 更多