Yii2:如何映射CSV字符串的形式的属性的CheckBoxList? [英] Yii2: How to map a CSV string in an attribute to CheckboxList in a form?

查看:106
本文介绍了Yii2:如何映射CSV字符串的形式的属性的CheckBoxList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此持有CSV字符串的属性模型。

I have a model with an attribute that holds a CSV string.

(该模型实际上是一个ActiveRecord的对象,但我想这并不重要。纠正我,如果我错了。)

(The model is actually an ActiveRecord object but I guess this is not important. Correct me if I'm wrong.)

/**
 * @property string $colors Can be something like "red" or "red,green,blue" or ""
 */
class Product extends Model {        
}

和我有一个在我想显示这个属性为的的CheckBoxList ,使用户可以通过简单的点击,而不是输入到TextInput选择可能的值。

And I have a form in which I'd like to display this attribute as a checkboxList so that the user can select the possible values with simple clicks instead of typing into a textInput.

从理论上讲,它应该类似于此:

Theoretically, it should look similar to this:

<?php $availableColors = ['red' => 'Red', 'green' => 'Green', 'blue' => 'Blue']; ?>

<?php $form = ActiveForm::begin([]); ?>
    <?= $form->field($model, 'colors')->checkboxList($availableColors) ?>
<?php ActiveForm::end(); ?>

这也显然不能,因为外地颜色将需要一个数组工作。但在我的模型,它是一个字符串。

This does obviously not work since the field colors would need to be an array. But in my model it is a string.

什么是实现这一目标的好方法?随着JS或伪属性?该颜色属性不能,因为它改变了已经被用在不应该被修改了其他环境。

What would be a good way to achieve that? With JS or pseudo attributes? The colors attribute must not be changed since it is already used in other contexts that shouldn't be modified.

推荐答案

现在我解决了它与形式的额外的模型。这在我看来,一个妥善的解决办法。

Now I solved it with an extra model for the form. This seems to me a proper solution.

/**
 * @property string $colors Can be something like "red" or "red,green,blue" or ""
 */
class Product extends Model {
}

/**
 * @property string[] $colorsAsArray
 */
class ProductForm extends Product {

    public function rules() {
        return array_merge(parent::rules(), [
            ['colorsAsArray', 'safe'] // just to make it possible to use load()
        ]);
    }

    public function getColorsAsArray() {
        return explode(',', $this->colors);
    }

    public function setColorsAsArray($value) {
        $this->colors = self::implode($value);
    }

    protected static function implode($value) {
        if ($value == 'none-value') return '';
        return implode(',', $value);
    }

    /* - - - - - - - - - - optional - - - - - - - - - - */

    public function attributeLabels() {
        $attributeLabels = parent::attributeLabels();
        return array_merge($attributeLabels, [
            'colorsAsArray' => $attributeLabels['colors'],
        ]);
    }
}

有了这个,我可以使用的形式,方式:

With this I can use the form that way:

<?php $availableColors = ['red' => 'Red', 'green' => 'Green', 'blue' => 'Blue']; ?>

<?php $form = ActiveForm::begin([]); ?>
    <?= $form->field($model, 'colorsAsArray')
             ->checkboxList($availableColors, ['unselect' => 'none-value']) ?>
<?php ActiveForm::end(); ?>

当然,现在的控制器使用继承的模型类。

Of course, now the controller has to use the inherited model class.

该解决方案也涉及这个问题,如果没有复选框被选中。这就是为什么没有价值介绍。

The solution deals also with the issue if no checkbox is selected. That is why 'none-value' is introduced.

这篇关于Yii2:如何映射CSV字符串的形式的属性的CheckBoxList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆