第一次提交时未提交 CKEDITOR 字段中的文本
Text in CKEDITOR field not being submitted in the first submission
第一次提交表单时ckeditor
字段中的文本没有发送(仅在第二次,第三次等)。
例如,如果尝试创建文章的 post 并提交表单,我将收到验证错误:'The field body
is required'。如果再次尝试提交(第二次或第三次),它会成功。
真正的问题是在编辑的时候!例如,当编辑一个表单时,字段 'body',以及其他字段,是用数据库中的数据填写的.换句话说,ckeditor 字段中已经有文本。
如果我第一次尝试提交表单 它不会更新 body
因为 ckeditor
中的文本没有发送;发送的是默认值(旧文章的body
,填的是DB的数据)
因此,除非我在其他字段中收到验证错误,否则它不会编辑(如果我收到验证错误,我将不得不再次提交,这将起作用)。
如何解决这个问题?这是 CKEDITOR
4 中的已知错误吗?如果我不解决这个问题,如果用户必须至少提交两次表单才能编辑或创建文章,他们会感到沮丧。
这是我正在使用的插件列表(可能对解决问题有用):
a11yhelp,关于,api,自动完成,自动更正,浏览器,剪贴板,colordialog,复制格式,交叉引用,对话框,div,docprops,查找,谷歌搜索,图像,link , liststyle, magicline, mathjax, openlink, pastecode, pastfromword, preview, quicktable, scayt, section, showblocks, sourcedialog, specialchar, table, table选择, table工具,table工具栏,文本转换,小部件,wsc
顺便说一句,我在他们的官方网站上使用 ckeditor
生成器下载了 ckeditor
。
我在 GitHub 中打开了这个问题,一个人解决了这个问题。他提出的解决方案效果很好!他是这样说的:
Workaround
As the issue is more tricky to fix than it seems, for now I propose a
simple workaround: invoke ajaxRequest
not on $( document ).ready
,
but rather on editor's loaded event:
CKEDITOR.replace( 'editor', {
on: {
loaded: function() {ajaxRequest();}
}
});
Explanation of the issue
The issue is connected with how DOM listeners are registered for given
element:
The order of event listeners for a particular event type will always
be:
- The event listeners registered with
addEventListener()
before the first time the event handler's value was set to non-null
- Then the callback to which it is currently set, if any
- Finally, the event listeners registered with
addEventListener()
after the first time the event handler's value was set to non-null.
In case of CKEditor 4, the value of the form's element is modified by
editor._attachToForm private method, which adds event listener to
form's submit event:
ckeditor-dev/core/editor.js
form.on( 'submit', onSubmit );
However this listener is added on loaded event, which is fired
asynchronously when editor is loaded – so after registering
synchronous onsubmit handler with the validation logic. This way
editor's field is updated after validating.
Proposed solutions
Update editor's element on formdata
event. This way we would have
total control over data being submitted and we would be sure that
correct data is set before submit event. The problem with this
solution is the fact that browsers' support is non-existent; the event
will appear in Chrome 77, however it is still not known if and when
the support will appear in Firefox or Safari.
Update editor's element on every change in the editor's content thanks
to change event. This solution will also fix cases, where some other
scripts are using value not from the editor, but directly from the
replaced textarea
– they would get fresh data more often then only
after submitting the form. However this solution requires #1364, which
connects with a pretty big refactoring.
注意:AjaxRequest
是我用来与 Jquery
一起提交表单的函数。
第一次提交表单时ckeditor
字段中的文本没有发送(仅在第二次,第三次等)。
例如,如果尝试创建文章的 post 并提交表单,我将收到验证错误:'The field body
is required'。如果再次尝试提交(第二次或第三次),它会成功。
真正的问题是在编辑的时候!例如,当编辑一个表单时,字段 'body',以及其他字段,是用数据库中的数据填写的.换句话说,ckeditor 字段中已经有文本。
如果我第一次尝试提交表单 它不会更新 body
因为 ckeditor
中的文本没有发送;发送的是默认值(旧文章的body
,填的是DB的数据)
因此,除非我在其他字段中收到验证错误,否则它不会编辑(如果我收到验证错误,我将不得不再次提交,这将起作用)。
如何解决这个问题?这是 CKEDITOR
4 中的已知错误吗?如果我不解决这个问题,如果用户必须至少提交两次表单才能编辑或创建文章,他们会感到沮丧。
这是我正在使用的插件列表(可能对解决问题有用):
a11yhelp,关于,api,自动完成,自动更正,浏览器,剪贴板,colordialog,复制格式,交叉引用,对话框,div,docprops,查找,谷歌搜索,图像,link , liststyle, magicline, mathjax, openlink, pastecode, pastfromword, preview, quicktable, scayt, section, showblocks, sourcedialog, specialchar, table, table选择, table工具,table工具栏,文本转换,小部件,wsc
顺便说一句,我在他们的官方网站上使用 ckeditor
生成器下载了 ckeditor
。
我在 GitHub 中打开了这个问题,一个人解决了这个问题。他提出的解决方案效果很好!他是这样说的:
Workaround
As the issue is more tricky to fix than it seems, for now I propose a simple workaround: invoke
ajaxRequest
not on$( document ).ready
, but rather on editor's loaded event:
CKEDITOR.replace( 'editor', {
on: {
loaded: function() {ajaxRequest();}
}
});
Explanation of the issue
The issue is connected with how DOM listeners are registered for given element:
The order of event listeners for a particular event type will always be:
- The event listeners registered with
addEventListener()
before the first time the event handler's value was set to non-null- Then the callback to which it is currently set, if any
- Finally, the event listeners registered with
addEventListener()
after the first time the event handler's value was set to non-null.In case of CKEditor 4, the value of the form's element is modified by editor._attachToForm private method, which adds event listener to form's submit event:
ckeditor-dev/core/editor.js
form.on( 'submit', onSubmit );
However this listener is added on loaded event, which is fired asynchronously when editor is loaded – so after registering synchronous onsubmit handler with the validation logic. This way editor's field is updated after validating.
Proposed solutions
Update editor's element on
formdata
event. This way we would have total control over data being submitted and we would be sure that correct data is set before submit event. The problem with this solution is the fact that browsers' support is non-existent; the event will appear in Chrome 77, however it is still not known if and when the support will appear in Firefox or Safari.Update editor's element on every change in the editor's content thanks to change event. This solution will also fix cases, where some other scripts are using value not from the editor, but directly from the replaced
textarea
– they would get fresh data more often then only after submitting the form. However this solution requires #1364, which connects with a pretty big refactoring.
注意:AjaxRequest
是我用来与 Jquery
一起提交表单的函数。