Search code examples
javascriptsymfonysonata-adminsymfony-sonata

Symfony2 - Sonata Admin - add javascript before field


In admin class:

protected function configureFormFields(FormMapper $formMapper) {
    $formMapper
            ->add('name', 'text')
            ->add('description', 'text')
    ;
}

I don't know how I can before "name" add javascript, can you help me?


Solution

  • Working for me:

    In admin class src\PP\TestBundle\TestAdmin.php

    public function configure() {
        $this->setTemplate('edit', 'PPTestBundle:CRUD:edit_javascript.html.twig');
    }
    

    In src\PP\TestBundle\Resources\views\edit_javascript.html.twig

    {% extends 'SonataAdminBundle:CRUD:base_edit.html.twig' %}
    {% block javascripts %}
    {{ parent() }}
    <script src="{{ asset('bundles/pptest/admin/js/myscripts.js') }}" type="text/javascript"></script>
    {% endblock %}
    

    When you do all this stuff and you have upload myscripts.js you should send this in command line:

    app/console assets:install web
    

    (possible that I forgot something)

    Sorry for my bad English :<>