Symfony2 Form: Currency Symbol and Display in Twig

I have a form in Symfony that uses a currency field type with the currency set to GBP.

When I submit the form to twig the form is displayed with:

{{ form_row(form.price) }}

      

This displays the following html:

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

      

My goal is to try and get the currency symbol so that I can use the bootstrap 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 styles I need to do. However, I tried to get the currency symbol using below, but there doesn't seem to be any documentation I can see to do this.

{{ form_label(form.price) }}

{{ form_widget(form.price) }}

      

None of them seem to work to get the currency and I also considered using {{ form_widget(form.price.vars.currency) }}

that just shows errors.

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

+3


source to share


2 answers


IMHO there is no special method to get the currencys character ( check the reference for form variables ). Default value for currency: type: string default: EUR (which correspond to ISO 4217 code ).

Thus, you can simply set the currency to be empty and the symbol will not be displayed:



$builder
->add('price', MoneyType::class, array('currency'=>''));

      

+4


source


You can show the currency symbol with this twig regex:



{{ form.price.vars.money_pattern | replace({ '{{ widget }}':''}) | raw }}

      

0


source







All Articles