Reputation: 3576
I am trying to create a page where the user can create a batch of new invoice records in the database. Each Member has both "Default Amount" and "Default currency" properties assigned in the entity. For the default currency, this is stored in the database as either E
(Euros) or D
(Dollars).
However, in the page to create the new invoices, the "Default amount" is populated correctly for each member, however the select box for currency only ever shows "Euro" even if the member has Defaultinvoicecurrency=D
. This is because selected
is never being added to any of the select options of what is stored in the database for the Members.
If the form is submitted with a currency selected from the dropdown then these are correctly added to the database (E
or D
).
Member Entity:
/**
* @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;
Invoice entity:
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;
Controller:
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,
))
/* ... */
}
}
Upvotes: 1
Views: 1990
Reputation: 2710
As you are passing the Invoice entity instance to your form, the correct entity data field is 'currency'. I.e. you need to remove the superfluous 'data' definition from that form field:
->add('currency',ChoiceType::class, array(
'choices' => array('Euro' => 'E', 'US Dollar' => 'D'),
'choices_as_values' => true,
))
Upvotes: 1