3

I've got a form in Symfony that uses the money field type with the currency set to GBP.

When I pass the form over to twig the form is rendered using:

{{ form_row(form.price) }}

This renders the following html:

£<input id="app_product_price" name="app_product[price]" required="required" class="form-control" value="9.95" type="text">

My aim is to try and get the currency symbol so I can use the bootstrap money field:

<form class="form-inline">
  <div class="form-group">
    <label class="sr-only" for="exampleInputAmount">Amount (in dollars)</label>
    <div class="input-group">
      <div class="input-group-addon">$</div>
      <input type="text" class="form-control" id="exampleInputAmount" placeholder="Amount">
      <div class="input-group-addon">.00</div>
    </div>
  </div>
  <button type="submit" class="btn btn-primary">Transfer cash</button>
</form>

Obviously, there are some styling that I need to do. However, I've tried to get the currency symbol using the below but there doesn't seem to be any documentation that I can see to do this.

{{ form_label(form.price) }}

{{ form_widget(form.price) }}

Neither of these seem to work to get the currency and I have also had a look into using {{ form_widget(form.price.vars.currency) }} which just shows errors.

Are there any known methods of extracting the currency symbol from the form field? Or will I need to go down the route of Twig Form Theming?

Liam Sorsby
  • 2,912
  • 3
  • 28
  • 51

2 Answers2

7

IMHO there is no specific method to get the currency’s symbol (check the Form Variables Reference). The default value for the currency is : type: string default: EUR (which correspond to the ISO 4217 code).

So you can simply set the currency to empty, and no symbol will be rendering:

$builder
->add('price', MoneyType::class, array('currency'=>''));
Audrey Carval
  • 168
  • 2
  • 7
0

You can show the currency symbol with this twig regular expresion:

{{ form.price.vars.money_pattern | replace({ '{{ widget }}':''}) | raw }}
pablo.nunez
  • 124
  • 1
  • 5