kartik-v / yii2-markdown

Advanced Markdown editing and conversion utilities for Yii Framework 2.0
http://demos.krajee.com/markdown
Other
89 stars 41 forks source link

Editor does not work in ActiveForm #1

Closed chris68 closed 10 years ago

chris68 commented 10 years ago

The markdown editor does not really work inside an ActiveForm; whenever I press the save or preview button, the whole form submits.

Config

    'modules' => [
        'markdown' => [
            // the module class
            'class' => 'kartik\markdown\Module',
            // whether to use PHP SmartyPants to process Markdown output
            'smartyPants' => true

        ],

View

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use kartik\markdown\MarkdownEditor;

/**
 * @var yii\web\View $this
 * @var yii\widgets\ActiveForm $form
 */
?>

<?php
// Works
$value1 = 'TestOutsideForm';
echo MarkdownEditor::widget([
    'name' => 'markdown1', 
    'value' => $value1,
]);
?>
<?php $form = ActiveForm::begin(); ?>
// Does not work
$value2 = 'TestInsideForm';
echo MarkdownEditor::widget([
    'name' => 'markdown2', 
    'value' => $value2,
]);
 ?>
<?= Html::submitButton('Submit') ?>

<?php ActiveForm::end(); ?>
chris68 commented 10 years ago

The following code change (the return false) in kv-markdown.js fixed the problem:

    $(input).blur(function() {
        $(editor).removeClass('active');
    });

    $(preview).click(function() {
        togglePreview(params);
    return false; // Need to cancel the event - otherwise the button submits the form!!!!
    });

    $(save1).click(function() {
        genSaveFile('', params.saveHeader, $(input).val(), 'Text', params.saveText, params.url, params.nullMsg);
    });
chris68 commented 10 years ago

The following code change ( the $options['form'] = ) in MarkdownEditor.php fixed the problem with the other buttons:

        if ($markup) {
            $options['onclick'] = 'markUp(' . $btn . ', "#' . $this->inputOptions['id'] . '")';
            $options['form'] = 'markup'; // Refer to some non-existent form to disable the form submit
        }

However, this is just a hack. Could/should be done better with returning false....

kartik-v commented 10 years ago

Thanks for reporting. This will be resolved shortly. Does not need a javascript change... just a minor widget parameter change (need to explicitly set button type = button for the toolbar buttons -- HTML defaults it to submit).

kartik-v commented 10 years ago

Resolved via commit 48837f9 and b38ed74

chris68 commented 10 years ago

Works like charm. Thanks for the quick fix. And good to know about the defaults to submit thing....