How to pass a data like a database data or php constant data to display in a form in main.js

159 views Asked by At

We build a custom bundle follow with this instructor https://blog.sulu.io/how-to-develop-a-bundle-in-the-sulu-admin-1 We need to know how to pass data from a database to render radio input choices or dropdown select.

We try to create an add/edit form and in the form, we have a radio and dropdown we made with hardcore in HTML file in Vendor/TransportationBundle/Resources/public/js/components/transportation/form/form.html

The code is

<div class="grid-row">
    <label for="transportation-transportationType"><%= translations.transportationType %></label>
    <div class="custom-radio">
        <input name="transportationType" id="transportation-transportationType-1" type="radio"
               class="form-element content-type" value="1" data-mapper-property="transportationType">
        <span class="icon"></span>
    </div>
    <span class="clickable"><%= translations.private_shuttle %></span>
    <div class="custom-radio">
        <input name="transportationType" id="transportation-transportationType-2" type="radio"
               class="form-element content-type" value="2" data-mapper-property="transportationType">
        <span class="icon"></span>
    </div>
    <span class="clickable"><%= translations.shared_shuttle %></span>
    <div class="custom-radio">
        <input name="transportationType" id="transportation-transportationType-3" type="radio"
               class="form-element content-type" value="3" data-mapper-property="transportationType">
        <span class="icon"></span>
    </div>
    <span class="clickable"><%= translations.airplane %></span>
</div>

Is this have a way to change those radio to fetch the data from an array or a way to fetch the data from some controller action? or Have another way to use twig file with twig feature instead of html file?

Please provide a solution for us? Thank you

Sorry for my English.

1

There are 1 answers

0
Daniel Rotter On BEST ANSWER

You can e.g. check this Controller from the core. You can get data from wherever you want in the controller and pass it to the template:

<?php
class AcmeController {
    public function testAction() {
        $data = /* Get data somehow */;

        return $this->render('template', ['data' => $data]);
    }
}

Then you can access the data using the data twig variable in the rendered template.