如何限制redactor限制器插件上的max char?

时间:2017-01-23 09:52:43

标签: yii2 redactor

我在视图中有这段代码

<div class="col-xs-6">
        <?php echo $form->field($model, 'deskripsi_produk')->widget(Redactor::className(),['clientOptions'=>['autoresize'=>'true', 'limiter' => 20, 'plugins' => ['limiter'], 'buttons'=> ['html', 'formatting', 'bold', 'italic','underline','lists','horizontalrule'],]]);?>
        </div>

它可以限制20个字符但是如何将文本区域限制为模型上定义的最大字符串而不是限制特定数字?

这是我的模特

class TbProduk extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public  $image;
    public static function tableName()
    {
        return 'tb_produk';
    }

public function rules()
{
    return [
        ...
        [['deskripsi_produk'], 'string', 'max' => 2000],
        [['deskripsi_produk'], 'checkDesc'],
        ...
    ];
}
}

1 个答案:

答案 0 :(得分:0)

尝试通过rules获取价值是个糟糕的主意。相反,你应该在TbProduk中创建一个保持长度的常量:

class TbProduk extends \yii\db\ActiveRecord
{
    const DESKRIPSI_PRODUK_LENGTH = 2000;
    ...
    public function rules()
    {
        return [
            ...
            [['deskripsi_produk'], 'string', 'max' => static::DESKRIPSI_PRODUK_LENGTH],
            ...
        ];
    }
}

在你看来:

<div class="col-xs-6">
    <?php echo $form->field($model, 'deskripsi_produk')->widget(Redactor::className(),['clientOptions'=>['autoresize'=>'true', 'limiter' => TbProduk::DESKRIPSI_PRODUK_LENGTH, 'plugins' => ['limiter'], 'buttons'=> ['html', 'formatting', 'bold', 'italic','underline','lists','horizontalrule'],]]);?>
</div>