如何在Symfony表单中识别ChoiceType中的“selected”值

时间:2016-01-10 20:07:32

标签: forms symfony

我正在尝试创建一个页面,用户可以在该页面中创建一批新的发票记录。每个会员都有"默认金额"和"默认货币"在实体中分配的属性。对于默认货币,它以E(欧元)或D(美元)存储在数据库中。

但是,在创建新发票的页面中,"默认金额"为每个成员正确填充,但货币的选择框只显示"欧元"即使该成员具有 Defaultinvoicecurrency = D。这是因为永远不会将selected添加到存储在数据库中的成员的任何选项中。

enter image description here

如果使用从下拉列表中选择的货币提交表单,则会将这些货币正确添加到数据库(ED)。

会员实体:

/**
 * @var integer
 * @ORM\COLUMN(type="decimal", precision=7, scale=2)
 * @Assert\NotBlank
 * @Assert\Regex(
 *     pattern="/^\s*-?[1-9]\d*(\.\d{1,2})?\s*$/",
 *     match=true,
 *     message="Please check - amount should have up to two decimal places")
 */
protected $defaultinvoiceamount;

/** eg E=EUR or D=USD
 * @var string
 * @ORM\COLUMN(type="string", length=1)
 */
protected $defaultinvoicecurrency;

发票实体:

class Invoice
{
    /**
     * @ORM\ManyToOne(targetEntity="InvoiceBatch", inversedBy="invoice_ids")
     * @ORM\JoinColumn(name="invoicebatch_id", referencedColumnName="id")
     */
    protected $invoicebatch_id;

    /**
     * @ORM\ManyToOne(targetEntity="Member", inversedBy="invoice_ids",cascade={"persist"})
     * @ORM\JoinColumn(name="member_id",     referencedColumnName="id")
     */
    protected $member_id;

    /*
     * @var integer
     * @ORM\COLUMN(type="decimal", precision=7, scale=2)
     * @Assert\NotBlank
     * @Assert\Regex(
     *     pattern="/^\s*-?[1-9]\d*(\.\d{1,2})?\s*$/",
     *     match=true,
     *     message="Please check - amount should have up to two decimal places")
     */
    protected $amount;

    /**  eg PEN or USD
     * @var string
     * @ORM\COLUMN(type="string", length=1)
     */
    protected $currency;

控制器:

public function newInvoiceAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $members = $em->getRepository('AppBundle:Member')->findActiveMembers();

    $batch = new InvoiceBatch();
    foreach($members as $member) {
        $invoice=new Invoice();
        $invoice->setMemberId($member);
        $invoice->setAmount(   $member->getDefaultinvoiceamount()   );
        $invoice->setCurrency( $member->getDefaultinvoicecurrency() );
        $invoice->setinvoicebatchid($batch);
        $batch->addInvoiceId($invoice);
    }

    $form=$this->createForm(InvoiceType::class, $invoice);
    $form->handleRequest($request);
    if ($form->isSubmitted() && ($form->isValid())) {
    /* ... */
}

FormType:

class InvoiceType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('amount', MoneyType::class, array(
                'label' => 'Amount',
            ))

            ->add('currency',ChoiceType::class, array(
                'data'=> 'Defaultinvoicecurrency',
                'choices' => array('Euro' => 'E', 'US Dollar' => 'D'),
                'choices_as_values' => true,
            ))
    /* ... */
    }
}

1 个答案:

答案 0 :(得分:1)

当您将Invoice实体实例传递给表单时,正确的实体数据字段为' currency'。即你需要删除多余的数据'该表格字段的定义:

->add('currency',ChoiceType::class, array(
    'choices' => array('Euro' => 'E', 'US Dollar' => 'D'),
    'choices_as_values' => true,
))