Search code examples
phpvalidationyiimultiple-records

yii multiple record validate and insert


I want to validate and insert multiple records from single form.

I tried following solution, but it is not validating each records. Yii - multiple records in one form submission

I've used something like this in my form:

<td><?php echo CHtml::activeTextField($item,"[$i]name"); ?></td>
<td><?php echo CHtml::activeTextField($item,"[$i]price"); ?></td>
<td><?php echo CHtml::activeTextField($item,"[$i]count"); ?></td>
<td><?php echo CHtml::activeTextArea($item,"[$i]description"); ?></td>

And in my controller I've done similar to this:

public function actionBatchCreate() {
    $models=array();
    // since you know how many models
    $i=0;
    while($i<5) {
        $models[]=Modelname::model();
        // you can also allocate memory for the model with `new Modelname` instead
        // of assigning the static model
    }
    if (isset($_POST['Modelname'])) {
        $valid=true;
        foreach ($_POST['Modelname'] as $j=>$model) {
            if (isset($_POST['Modelname'][$j])) {
                $models[$j]=new Modelname; // if you had static model only
                $models[$j]->attributes=$model;
                $valid=$models[$j]->validate() && $valid;
            }
        }
        if ($valid) {
            $i=0;
            while (isset($models[$i])) {
                $models[$i++]->save(false);// models have already been validated
            }
            // anything else that you want to do, for example a redirect to admin page
            $this->redirect(array('modelname/admin'));
        }
    }

    $this->render('batch-create-form',array('models'=>$models));
}  

Solution

  • <?php
    public function actionBatchCreate() 
    {
    
        $arrItems = array();
        $valid = true;
    
        if(isset($_POST['Modelname'])) {
    
            foreach ($_POST['Modelname'] as $i => $notUsed)
            {
                $objItem = new Modelname();
                $objItem->attributes=$_POST['Modelname'][$i];
    
                $valid = $objItem->validate() && $valid;
                $arrItems[] = $objItem;
            }
    
            if($valid) {
                foreach ($arrItems as $objItemValidated) {
                    $objItemValidated->save();  
                }
    
                $this->redirect(array('modelname/admin'));
            }
        }
    
        // optional create a initial empty row in View
        if(!count($arrItems)) {
            $arrItems[] = new Modelname();
        }   
    
        $this->render('batch-create-form',array('models'=>$arrItems));
    
    }  
    

    View-File batch-create-form.php

    <table>
    <?php foreach($models AS $i => $item):?>
        <tr>
            <td><?php echo CHtml::activeTextField($item,"[$i]name"); ?></td>
            <td><?php echo CHtml::activeTextField($item,"[$i]price"); ?></td>
            <td><?php echo CHtml::activeTextField($item,"[$i]count"); ?></td>
            <td><?php echo CHtml::activeTextArea($item,"[$i]description"); ?></td>
        <tr>
    <?php endforeach;?>
    </table>