dsc6517 2016-10-12 07:09
浏览 22
已采纳

在yii2中显示两个模型项作为无线电列表,其中用户不能同时选择两个

I have a model having two items : is_normal and is_transporter

I would like to show both in an active form as radio list but i havent figured out how

This is the model:

 public function rules()
{
    return [
        [['reg_no', 'truck_category', 'added_by', 'truck_status', 'driver_name'], 'required'],
        [['truck_category', 'added_by', 'truck_status', 'is_normal', 'is_transporter'], 'integer'],
        [['added_on'], 'safe'],
        [['reg_no'], 'string', 'max' => 50],
        [['driver_name'], 'string', 'max' => 100],
        [['truck_category'], 'exist', 'skipOnError' => true, 'targetClass' => TblChecklistCategory::className(), 'targetAttribute' => ['truck_category' => 'id']],
        [['added_by'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['added_by' => 'id']],
        [['truck_status'], 'exist', 'skipOnError' => true, 'targetClass' => TblTruckStatus::className(), 'targetAttribute' => ['truck_status' => 'id']],
    ];
}

How can i use the two model attributes in active form as a radiolist

  • 写回答

1条回答 默认 最新

  • douzhuo3233 2016-10-12 08:13
    关注

    I would avoid having two attributes for this when one is enough but if you really need this here is the answer.

    Prepare virtual attribute in the model to handle the radio button.

    /**
     * @var boolean
     */
    public $normal_or_transporter;
    

    You need to add it in the rules:

    public function rules()
    {
        return [
            // ...
            [['reg_no', 'truck_category', 'added_by', 'truck_status', 'driver_name', 'normal_or_transporter'], 'required'],
            ['normal_or_transporter', 'boolean'],
        ];
    }
    

    Now in the form view add this field (assuming standard ActiveForm widget):

    <?= $form->field($model, 'normal_or_transporter')->radioList([
        0 => 'Normal truck',
        1 => 'Transporter'
    ]) ?>
    

    Final step is to handle the virtual attribute. You can do it in the model again. We can use beforeSave() because is_normal and is_transporter are not in the required rule so validation pass without them being set.

    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            $this->is_normal = 0;
            $this->is_transporter = 0;
            if ($this->normal_or_transporter) {
                $this->is_transporter = 1;
            } else {
                $this->is_normal = 1;
            }
            return true;
        }
        return false;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 esp8266控制共阳极wrgb灯板无法关闭所有led灯
  • ¥100 python读取速度问题
  • ¥15 stm32f407使用DMA问题
  • ¥15 您好 这个API接口该怎么弄 网站搭建好了 API也有 现在就不知道该怎么填写API 不知道怎么用
  • ¥88 用uniapp写一个多端的程序,用到高德地图,用高德的JSAPI吗?
  • ¥20 关于#c++#的问题:水果店管理系统
  • ¥30 dbLinq最新版linq sqlite
  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 完成虚拟机环境配置,还有安装kettle
  • ¥15 有人会搭建生鲜配送自营+平台的管理系统吗