Symfony表单使用IvoryCKEditorBundle为CKEditor字段提交空值

时间:2014-05-12 22:51:48

标签: php forms symfony ckeditor

我正在使用标准的Symfony 2.4表单类,IvoryCKEditorBundle。配置是:

ivory_ck_editor:
    default_config: cms
    configs:
        cms:
            toolbar: standard

作为参考,我有一个$view实体,其中有一个关联的$viewVersion实体,其中CKeditor位于$view->getVersion()->getContent()。 (但就提交数据而言,实体的结构方式应该没有任何区别,但是如果你提出要求的话。)

我的表单在我的控制器中像任何普通表单一样创建,调用预定义类型:

$form = $this->createForm(new ViewType(), $view);

ViewType中,使用默认配置创建字段:

$builder
    ->add('content', 'ckeditor', array(
    'label' => false,
    'required' => false
    ));

CKeditor在浏览器中显示我的表单的content字段(欢呼!),但是当我提交表单时,内容字段中的数据未提交。内容字段仍为空...不在原始$_POST或已清理的$request->request->all()或表单数据$form->getData()->getContent()中。

Array (
    [view] => Array (
        [status] => 1
        [version] => Array (
            [title] => My Title
            [content] => 
        )
        [lockVersion] => 1
        [save] => 
        [_token] => xxxxxxxxxxx
    )
)

似乎CKEditor的javascript应该为此字段更新表单的隐藏textarea(如此AJAX related question所述),但这没有发生,因此它提交为空。如果我使用内容字段的值来填充我的实体,例如$ view-> getVersion() - > setContent('Default Content')即使我在CKEditor中输入了另一个字符串,它仍然保留在数据库中。

我认为这个捆绑包应该只是“开箱即用”,所以我不确定我做错了什么。

那么CKEditor如何在这个捆绑中工作? 是否应该通过javascript动态更新隐藏的textarea字段?因为没有发生......

我创建了一个只有两个ckeditor字段的简化演示表单,如果你想看到它的实际效果:[link不再有效]

2 个答案:

答案 0 :(得分:1)

查看为textarea生成的HTML。 <textarea>包含在div元素中,该元素与textarea本身具有相同的ID。这使得ckeditor对要更新的元素感到困惑。

我的猜测是你正在使用bootstrap,symfony的bootstrap包覆盖表单模板以添加类,这就是冲突的来源。

根据您的代码,我会添加如下内容:

{% block widget_container_attributes %}
    {% if id is not empty %}
       {% set id = id ~ '_container' %}
    {% endif %}
    {% set attr = attr|merge({'class': (attr.class|default('') ~ ' form-group')|trim}) %}
    {{ parent() }}
{% endblock widget_container_attributes %}

如果你要大量使用bootstrap,你也可以考虑this bundle

答案 1 :(得分:0)

我在后端使用了这个捆绑包。您是否在config.yml文件中配置了此捆绑包?这是我的配置。

ivory_ck_editor:
    default_config: cke_config
    configs:
        cke_config:
            filebrowserBrowseRoute: admin_sonata_media_media_browser
            filebrowserImageBrowseRoute: admin_sonata_media_media_browser
            # Display images by default when clicking the image dialog browse button
            filebrowserImageBrowseRouteParameters:
                provider: sonata.media.provider.image
            filebrowserUploadRoute: admin_sonata_media_media_upload
            filebrowserUploadRouteParameters:
                provider: sonata.media.provider.file
            # Upload file as image when sending a file from the image dialog
            filebrowserImageUploadRoute: admin_sonata_media_media_upload
            filebrowserImageUploadRouteParameters:
                provider: sonata.media.provider.image
                context: default-context # Optional, to upload in a custom context
            allowedContent : true

捆绑包有选项allowedContent : true。我认为它会对你有帮助。