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

Provide widget for use in activeform/activefield #10

Closed chris68 closed 10 years ago

chris68 commented 10 years ago

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,
                ]) ?>
chris68 commented 10 years ago

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

kartik-v commented 10 years ago

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.

kartik-v commented 10 years ago

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

kartik-v commented 10 years ago

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.

chris68 commented 10 years ago

Checking. Works well except for two issues

kartik-v commented 10 years ago

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.

chris68 commented 10 years ago

Works perfect!