3

in short:

  1. i'm using Yii Framework

  2. i have a one Ckeditor window on my page ( php/ yii framework - works fine)

  3. when i hit a button, a new CKeditor window is being generated and shown through AJAX call

  4. THE PROBLEM: this new CKEditor window correctly displays the text stored in the database BUT : when i hit "Save" (an ajax button generated together with the rest of the form) the values from this new CKeditor window will not save : CKeditor sends back the old values that it got from the database.

When i remove the Ckeditor and leave the plain <textarea> : everything is ok so i know that the controller is fine.

Please, anybody went through something like this?

ok_computer
  • 67
  • 1
  • 2
  • 8

2 Answers2

2

You can let CKEDITOR update the textarea before validating, and clientside/ajax validation will work as expected:

<?php $form = $this->beginWidget('CActiveForm', array(
    'enableAjaxValidation' => true,   // one or both
    'enableClientValidation' => true, // one or both
    'clientOptions' => array(
        'validateOnSubmit' => true,   // optional
        'beforeValidate' => new CJavaScriptExpression('function(form) {
            for(var instanceName in CKEDITOR.instances) { 
                CKEDITOR.instances[instanceName].updateElement();
            }
            return true;
        }'),
    ),
)); ?>
marcovtwout
  • 5,230
  • 4
  • 36
  • 45
2

Sounds like a typical post-AJAX JS binding issue. :) There are a few possibilities for how to fix it, depending on what is going wrong.

This post in the Yii forum should be money for you, it's where I got most of these suggestions: http://www.yiiframework.com/forum/index.php?/topic/9341-ckeditor-widget-in-a-cactiveform/

  1. Use a widgetized Yii extension which has already solved this problem (NHCKEditor?)
  2. Add an onClick callback to the submit button which saves the CKEditor content to the hidden 'textarea' ('onclick'=>'CKEDITOR.instances.TEXTAREA_ID.updateElement()',
  3. Use jQuery to get the data from the CKEditor iFrame to use... wherever. AJAX validation, etc.

Good luck!

Community
  • 1
  • 1
thaddeusmt
  • 15,410
  • 9
  • 67
  • 67
  • Thank you! The problem was in 2 places: the ajaxSubmitButton and the textarea name: because all the ckeditor instances were using the same model but with different rows, they all had the same name, so as the ajaxSubmitButton. I used the 'onclick' event to manually copy the contents of the ckeditor window to the textarea and introduced additional name for each of the repeating instances :D Works fine now, thugh i decided to let go the ajax loading - i just couldn't make it work, now they're just rendered into separate JuiTabs – ok_computer May 28 '11 at 13:40
  • Yii::app()->clientscript->registerScript('duppa'.$thisLanguageID," function beforeCommentValidate".$thisLanguageID."() { var t = CKEDITOR.instances['".CHtml::activeName($model, 'description_translation')."[".$thisLanguageID."]'].getData(); $('textarea[name=\"".CHtml::activeName($model, 'description_translation')."[".$thisLanguageID."]\"]').val(t); return true; } $('#closeTlumaczenieDialog".$thisLanguageID."').click(function(){beforeCommentValidate".$thisLanguageID."();}); "); – ok_computer May 28 '11 at 13:46