I
I
Ilya2015-07-11 19:16:47
MySQL
Ilya, 2015-07-11 19:16:47

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`),

To it the standard form
3a683a5cd6e945e6aba780cdd675e97a.png
Current controller
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();     
    }

Form Generator
<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>

How can I go over each line with a set of fields and validate with error output to the form. Ideally, also check if there is data in the database (there is already a line in the picture), then UPDATE if there is no data, then INSERT.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Bay, 2015-07-11
@kawabanga

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 question

Ask a Question

731 491 924 answers to any question