K
K
Konstantin2017-06-26 15:34:00
Yii
Konstantin, 2017-06-26 15:34:00

How to make each checkbox its own input in Yii2?

Hello. For an online store, I'm trying to make this thing 26463cd0a92de6b122fb03576aa8ccb7.pnghtml is displayed, but I can't validate it.
Model

class AddGoodsForm extends Model{
    
    public $name;
    public $price;
    public $file;
    public $imageUrl;
    public $count;
    public $stores;
    public $count_stores;
    
    public $categoryId;
    
    public function rules(){
        return [
            [['name', 'price', 'count'], 'required', 'message'=>'Обязательно к заполнению'],
            
            [['price', 'categoryId', 'count', 'stores', 'count_stores'], 'integer'],
            
            ['file', 'file', 'extensions'=>'png, jpg']
        ];
    }
    public function save(){
        $self = new Products();
        $self->name = $this->name;
        $self->price = $this->price;
        $self->count = $this->count;
        $self->categoryId = $this->categoryId;
        $self->imageUrl = $this->imageUrl;
//        $self->save();
        
        var_dump($this->stores);
        var_dump($this->count_stores);
    }
}

Part of the view where I display the form
echo $form->field($model, 'stores')->label('Магазины')->checkboxList(\yii\helpers\ArrayHelper::map(\app\models\Store::find()->all(), 'id', 'name'),
    ['item' => 
        function($index, $label, $name, $checked, $value) {
            return "
                <table width=300>
                    <tr>
                        <td style='width:200px;'>".Html::checkbox('stores',  $checked, [
                                                'value' => $value,
                                                'label' => $label,
                                                'class' => 'any class',])."</td>
                    <td>
                    ".Html::input('text', 'count_stores')."
                    </td>
                    </tr>
                </table>
            ";
        }
    ]);

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Maxim Fedorov, 2017-06-26
@qonand

To build such a thing, it is better to use a ready-made extension, for example, here and not to do it yourself ...

I
Igor Vasiliev, 2017-08-09
@Isolution666

Use "checkboxTemplate" - it's more appropriate here:
www.yiiframework.com/doc-2.0/yii-bootstrap-activef...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question