Answer the question
In order to leave comments, you need to log in
Yii. How to validate multiple sets of fields in a MySQL array and display errors in a form?
Good day to all
There is a table:
CREATE TABLE `goods_variations` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`goods_id` INT(10) UNSIGNED NOT NULL,
`articul` VARCHAR(100) NULL DEFAULT NULL,
`size` VARCHAR(100) NULL DEFAULT NULL,
`color` VARCHAR(100) NULL DEFAULT NULL,
`weight` VARCHAR(100) NULL DEFAULT NULL,
`stock` INT(5) NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `FK_goods_options_goods` (`goods_id`),
INDEX `articul` (`articul`),
public function actionVariations($id) {
if (Yii::app()->request->isAjaxRequest) {
$this->layout = 'blank';
$goods = Goods::model()->findByPk($id);
$variations = GoodsVariations::model()->findAll(array('condition'=>'goods_id='.$id));
if (isset($_REQUEST['GoodsVariations'])) {
foreach ($_REQUEST['GoodsVariations'] as $values) {
$variation = new GoodsVariations;
$values['goods_id'] = $goods->id;
$variation->attributes = $values;
$variation->save();
print_r($_REQUEST);
}
}
$this->render('_variations',array('variations'=>$variations, 'goods'=>$goods));
} else {
throw new CHttpException('403', 'Forbidden access.');
}
Yii::app()->end();
}
<table class="table table-bordered">
<thead>
<tr>
<th>Артикул</th>
<th>Размер</th>
<th>Цвет</th>
<th>Вес</th>
<th>Количество</th>
</tr>
</thead>
<tbody>
<?php
$i=0;
foreach ($variations as $variation) {
echo '<tr>';
echo '<td>'.CHtml::activeTextField($variation,'['.$i.']articul',array('size'=>12,'class'=>'form-control')).'</td>';
echo '<td>'.CHtml::activeTextField($variation,'['.$i.']size',array('size'=>12,'class'=>'form-control')).'</td>';
echo '<td>'.CHtml::activeTextField($variation,'['.$i.']color',array('size'=>12,'class'=>'form-control')).'</td>';
echo '<td>'.CHtml::activeTextField($variation,'['.$i.']weight',array('size'=>12,'class'=>'form-control')).'</td>';
echo '<td>'.CHtml::activeTextField($variation,'['.$i.']stock',array('size'=>12,'class'=>'form-control')).'</td>';
echo '</tr>';
$i++;
}
?>
<tr>
<?php echo '<td>'.CHtml::textField('GoodsVariations['.$i.'][articul]','',array('size'=>12,'class'=>'form-control')).'</td>'; ?>
<?php echo '<td>'.CHtml::textField('GoodsVariations['.$i.'][size]','',array('size'=>12,'class'=>'form-control')).'</td>'; ?>
<?php echo '<td>'.CHtml::textField('GoodsVariations['.$i.'][color]','',array('size'=>12,'class'=>'form-control')).'</td>'; ?>
<?php echo '<td>'.CHtml::textField('GoodsVariations['.$i.'][weight]','',array('size'=>12,'class'=>'form-control')).'</td>'; ?>
<?php echo '<td>'.CHtml::textField('GoodsVariations['.$i.'][stock]','',array('size'=>12,'class'=>'form-control')).'</td>'; ?>
</tr>
</tbody>
</table>
Answer the question
In order to leave comments, you need to log in
www.yiiframework.com/doc/guide/1.1/ru/form.table
table input processing.
And to create or update, you can do it primitively. make hidden field with id position. if id does not come in post, then create a model, otherwise update.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question