Answer the question
In order to leave comments, you need to log in
How to make each checkbox its own input in Yii2?
Hello. For an online store, I'm trying to make this thing html 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);
}
}
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
To build such a thing, it is better to use a ready-made extension, for example, here and not to do it yourself ...
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 questionAsk a Question
731 491 924 answers to any question