kartik-v / yii2-markdown

Advanced Markdown editing and conversion utilities for Yii Framework 2.0

Home Page:http://demos.krajee.com/markdown

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Provide widget for use in activeform/activefield

chris68 opened this issue · comments

An activeWidget for the intended usage in an ActiveForm would be extremely helpful!

<?php $form = ActiveForm::begin(); ?>
  <?= $form->field($model, 'description')->widget('\kartik\markdown\MarkdownEditor', 
    [
        'showExport' => false,
    ]) ?>
<?php ActiveForm::end(); ?>

The widget then must fulfill the interface of the routine widget in class ActiveField, i.e. the parameter passing must be harmonized (instead of name and value use the model and attribute)

    /**
     * Renders a widget as the input of the field.
     *
     * Note that the widget must have both `model` and `attribute` properties. They will
     * be initialized with [[model]] and [[attribute]] of this field, respectively.
     *
     * If you want to use a widget that does not have `model` and `attribute` properties,
     * please use [[render()]] instead.
     *
     * @param string $class the widget class name
     * @param array $config name-value pairs that will be used to initialize the widget
     * @return static the field object itself
     */
    public function widget($class, $config = [])
    {
        /** @var \yii\base\Widget $class */
        $config['model'] = $this->model;
        $config['attribute'] = $this->attribute;
        $config['view'] = $this->form->getView();
        $this->parts['{input}'] = $class::widget($config);
        return $this;
    }
?>

Currently one has to use the cumbersome

            <?= $form->field($model, 'description')->widget('\kartik\markdown\MarkdownEditor', 
                [
                    'name' => Html::getInputName($model,'description'), 
                    'value' => $model->description,
                    'showExport' => false,
                ]) ?>

Besides: if one uses it like this widget the editor buttons do not work!

For use with a model within active form - use the following approach instead of your cumbersome approach above - this should achieve all your needs and you may not need the widget method for activeField.

use kartik\markdown\MarkdownEditor;
$form = ActiveForm::begin();
MarkdownEditor::widget([
     'model' => $model,
     'attribute' => 'description',
     'showExport' => false
]);
ActiveForm::end();

I will anyway check on the activefield widget method in the meanwhile and update if anything else can be enhanced.

Recording an issue for working with model. This will be rectified shortly.

Resolved via commit d6d1a95. You can use the widget with activefield this way:

echo $form->field($model, 'description')->widget(
    MarkdownEditor::classname(),
    ['showExport' => false]
);

Just clean your asset runtime folders and do a composer update to check. Thanks for reporting.

Checking. Works well except for two issues

  • Full screen mode no longer works! (Actually I might be a good idea to have an option to hide the button)
  • The textbox is rather small. How can I increase the size?

Resolved via commit c845306. Everything should work now and the widget should default the height to 260px.

Note you can control the textinput height by passing the height property.

You can hide/show toolbar buttons by passing the toolbar property. Just remember to pass the buttons correctly to the toolbar by referring to the setDefaultHeader() method.

Works perfect!