froala / django-froala-editor

Package to integrate Froala WYSIWYG HTML rich text editor with Django.
https://froala.com/wysiwyg-editor
283 stars 72 forks source link

Can't submit form because froala editor field is required but hidden #68

Open engin-can opened 5 years ago

engin-can commented 5 years ago

I have a problem where I can't submit my form because my description textarea is required but hidden in my form. Any idea how I can solve this? I have no idea where style="display:none;" is coming from. Any idea?

class CustomFroalaEditor(FroalaEditor):
    def __init__(self, *args, **kwargs):
        super(CustomFroalaEditor, self).__init__(*args, **kwargs)

    def render(self, name, value, attrs=None, renderer=None):
        html = super(FroalaEditor, self).render(name, value, attrs)
        return mark_safe(html)

class NewBlaForm(forms.ModelForm):
    description = forms.CharField(widget=CustomFroalaEditor(attrs={'id': 'nb__description-editor'}))

    class Meta:
        model = Bla
        fields = ('description')

image

tisdall commented 4 years ago

The form should still submit if you put content within the editor. The 'required' causes html5 validation which prevents submitting the form when it's blank. However, the browser is supposed to highlight the field, but it can't because it's hidden.

Here's a workaround:

from froala_editor.widgets import FroalaEditor as OriginalFroalaEditor

class FroalaEditor(OriginalFroalaEditor):
    def use_required_attribute(self, initial):
        # textarea is hidden so HTML5 validation won't work properly
        return False