EasyCorp / EasyAdminBundle

EasyAdmin is a fast, beautiful and modern admin generator for Symfony applications.
MIT License
4.08k stars 1.02k forks source link

Using a Type that contains script tag #5394

Closed bricebou closed 2 years ago

bricebou commented 2 years ago

Describe the bug I'm trying to use the EasyMDE Bundle (https://packagist.org/packages/reclic/easymde-bundle) that enhances textarea fields with a javascript markdown editor.

When the ReclicEasyMDEType is called, the following template is called :

{% block easymde_widget %}
    <textarea {{ block('widget_attributes') }}>{{ value }}</textarea>
    <script>
        {{ easymde }}.dispatchEvent(new CustomEvent('append', { bubbles: true }));
    </script>
{% endblock %}

But the script tag is removed when EasyAdmin renders the form.

To Reproduce Using EasyAdmin Bundle version 4: in a CrudController, I'm trying to call the ReclicEasyMDEType :

public function configureFields(string $pageName): iterable
    {
        return [
            TextareaField::new('description', 'Description')
                ->setFormType(ReclicEasyMDEType::class),
            CollectionField::new('quotes', 'Quotes')
                ->setEntryIsComplex(true)
                ->setEntryType(QuoteType::class)
        ];
    }

The Type/QuoteType.php contains this:

class QuoteType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('quote', ReclicEasyMDEType::class)
        ;
    }
}

The ReclicEasyMDEType template is loaded but the script tag is stripped ; here is what it's rendered:

<textarea id="Book_quotes_1_quote" name="Book[quotes][1][quote]" required="required" data-toolbar="heading, bold, italic, |, unordered-list, ordered-list, |, link, image, |, preview, side-by-side" class="form-control easymde form-control"></textarea>

There should be a script for each ReclicEasyMDEType...

Is there any option I can use to avoid the script content to be stripped ?

Thanks !

javiereguiluz commented 2 years ago

I don't know what to say. It looks very strange that the <script> tag disappears. EasyAdmin doesn't process Twig form templates in any way, because we rely on Symfony for this. I'm sorry but I'm confused about this, so I'm afraid I can't help you much 😐

bricebou commented 2 years ago

Hi,

Thanks for your reply ! I'll give it another try, working back on a personal project these days. But the <script> tag doesn't disappear when the ReclicEasyMDEType is called outside of EasyAdmin. I'll make more tests and come back here to post the outcome. Otherwise, maybe I'll find another way to add a Markdown editor within my forms.

Thanks again :-)

Guite commented 2 years ago

add a Markdown editor within my forms

How about CodeEditorField which is configured for Markdown by default (same as setting ->setLanguage('markdown') explicitly) ?

bricebou commented 2 years ago

How about CodeEditorField which is configured for Markdown by default (same as setting ->setLanguage('markdown') explicitly) ?

I'll give it a try, but I need to check how the data is stored... Thanks :-)

javiereguiluz commented 2 years ago

Let's close this because the built-in code editor field could be enough for this use case. Thanks.