tabular input error display

Can any one help how to display the error while using tabular input

For errorSummary:




$form->errorSummary(array_merge(array($model), $tabularModels));



You can pass an array to errorsummary, so the array of the models + the main model.

For the input fields:


$form->error($tabularModels[$i], 'attribute');

Is that what you need?

http://www.yiiframework.com/doc/guide/1.1/en/form.table

they given example… in this batch update model has validated it validates fine but it does not shows the error to appropriate field(to particular field)

In that they told to create array of object model i don’t know how to do this could you give me any full coding for this problem

There is an extension for tabular input, with full code example.

Try and tell me if something doesn’t work.




<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'mark-entry-form-test',

	'enableAjaxValidation'=>false,

)); 




//it will return no of subjects like c,c++,java for this i want to create form

$subject_list="1,3,5";

$subject_record=Subject::model()->findAll("subject_id IN ($subject_list)");




$exam_id=$_GET['exam_id'];

$enrollment_id=$_GET['enrollment_id'];


if($subject_record!=null)

{ ?>

<div class="form">

    <p class="note">Fields with <span class="required">*</span> are required.</p>

  

<table>

    <?php

    echo '<tr><td>'.$form->labelEx($model, 'enrollment_id');

    echo '</td><td>'.$enrollment_id.'</td></tr>';

    echo '<tr><td>'.CHtml::label('Student Name', 'student_name');

    echo '</td><td>'.$name.'</td></tr>';

    echo '<tr><td>'.CHtml::label('Held Date', 'held_date_selection');

    echo '</td><td>'.CHtml::textField('held_date_selection',"").'</td></tr>';


    //from this tabular records starts


    foreach($subject_record as $i=>$subject_rs)

    {

        $subject_id=$subject_rs['subject_id'];

        $subject_name=$subject_rs['subject_name'];

        $max_mark=$subject_rs['max_mark'];

        $pass_mark=$subject_rs['pass_mark'];

        //echo $subject_name;

        ?>


<tr>

<td><?php echo $form->labelEx($model,"[$subject_id]mark_obtained",array('label'=>$subject_name)); ?></td>

<td><?php


echo $form->hiddenField($model,"[$i]enrollment_id",array('value'=>$enrollment_id));

echo $form->hiddenField($model,"[$i]exam_id",array('value'=>$exam_id));

echo $form->hiddenField($model,"[$i]max_mark",array('value'=>$max_mark));

echo $form->hiddenField($model,"[$i]pass_mark",array('value'=>$pass_mark));

echo $form->hiddenField($model,"[$i]subject_name",array('value'=>$subject_name));

echo $form->textField($model,"[$i]mark_obtained"); ?></td>

</tr>

<?php } ?>

</table>

<?php echo CHtml::submitButton('save mark'); ?>

<?php

}


?>

</div>

<?php $this->endWidget(); ?>



and my controller is




public function actionMarkEntry()

	{


            if(isset ($_POST['Mark']))

            {

                $model=new Mark;

                $valid=true;

                foreach($_POST['Mark'] as $value)

                {

                   $value['held_date']=$_POST['held_date_selection'];

                    

                  // print_r($value);

                   $model->attributes=$value;

                   

                   $valid=$valid && $model->validate();

                  }

                  

		$this->render('markEntry');

	}



what’s the mistake i did?

You shoud use an array of model, not only a single model.

Take a look at the extension, is a fully operational manager for do this job.

In your example, you should so something like:




public function actionMarkEntry()

        {


            if(isset ($_POST['Mark']))

            {

                $models[]=new Mark;

                $valid=true;

                foreach($_POST['Mark'] as $value)

                {

                   $model[]->attributes=$value;

                   

                   $valid=$valid && $model->validate();

                  }

                  

                $this->render('markEntry', 'models'=>$models);

        }



And then use this $models for render the lines of your table

Thanks for your response


$model[]->attributes=$value;

i got this error

Call to a member function validate()

so i did little change


$models[]->attributes=$value;

and the complete code as follows and im getting this error

Call to undefined method stdClass::validate()




public function actionMarkEntry()

	{


            if(isset ($_POST['Mark']))

            {

                $models[]=new Mark;

                

                $valid=true;

                $i=0;

                foreach($_POST['Mark'] as $value)

                {

                   $value['held_date']=$_POST['held_date_selection'];

                   $value['mark_id']='';

                  // print_r($value);

                   $models[$i]->attributes=$value;

                   

                   $valid=$valid && $models[$i]->validate();

                 

                   $insertion_query="(";

                   $insertion_query.=$models[$i]->enrollment_id;

                   $insertion_query.=",".$models[$i]->exam_id;

                   $insertion_query.=",'".$models[$i]->subject_name."'";

                   $insertion_query.=",".$models[$i]->max_mark;

                   $insertion_query.=",".$models[$i]->pass_mark;

                   $insertion_query.=",'".$models[$i]->held_date."'";

                   $insertion_query.=",".$models[$i]->mark_obtained.")";

                   $insertion_query1[]=$insertion_query;

                   $i++;


                }

                if($valid)

                {

                    $sql="INSERT INTO mark (enrollment_id,exam_id,subject_name,max_mark,pass_mark,held_date,mark_obtained) VALUES".  implode(',', $insertion_query1);

                    $connection=Yii::app()->db;

                    $connection->createCommand($sql)->execute();

                    $this->redirect(array('create'));

                    unset ($insertion_query1);

                }


            }

		$this->render('markEntry',array('model'=>$models));

	}



Still not solved , can any one help me?

Is there any one can give us one example with full source code …

I hope that …