Tabular Input with CActiveForm

Does anyone have a complete example of using tabular input with CActiveForm?

I’m following the directions here, but they don’t take into account the CActiveForm. I can see the responses coming back for invalid entries in Firebug, but the form shows green (passed validation) on the fields.

I would like to figure this out as well. The problem seems to be that the CActiveForm validator (which popluates the model from the $_POST variables) isn’t aware of the $_POST[get_class($model)][$i] trick, so it can’t handle tabular input correctly. I might try and override the validate function to fix this? Not sure about my next move. It’s a bummer something in the Guide like Tabular input doesn’t work with the CActiveForm validator!

[SOLVED] - Didn’t read the article close enough. I documented my solution on Stackoverflow (can’t link it because I’m too new). It’s entitled “In Yii, is there a way to validate tabular input with CActiveForm?”

I’m running into the same problem. (I’ve been using Yii for a few weeks, I apologize in advance if there’s an elementary solution).

I did see a CActiveForm::validateTabular() in 1.1.10, but it looks like it’s for multiple models and not quite what I want to accomplish.

I’m writing a form to parse CSV “files” (temporarily stored in the DB). Before I actually parse out the CSV, I want to grab the first line (generally the CSV header) and have the user choose what header maps to what table/column.

For this header mapping form, I created ImportParseForm model extending CFormModel hoping I could use CActiveForm’s validation.

My controller pushes the number of the headers to the view, and my view renders that piece of the form this way:


<?php foreach($headers as $h => $hItem): ?>

<div class="row">

<?php echo CHtml::label(CHtml::encode($hItem), "[$h]header"); ?> maps to

<?php echo $fParse->textField($mForm, "[$h]header"); ?>

<?php echo $fParse->error($mForm, "[$h]header"); ?>

</div>

<?php endforeach; ?>

I thought I could do something like this in my ImportParseForm model:


    public function rules()

    {

        return array(

            array('header', 'required'),

        );

    }

This is how I thought I could validate it in the controller (note: ignoring AJAX validation at the moment):





            $mForm = new ImportParseForm;

            $valid = true;

            if (isset($_POST['ImportParseForm'])){                

                foreach ($headers as $h => $hVal){

                    if (isset($_POST['ImportParseForm'][$h])){

                        $mForm->attributes = $_POST['ImportParseForm'][$h];

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

                    }

                }




                if ($valid){

                    // Process stuff

                }

            }



The form is only valid if all fields are populated.

Looking into the CActiveForm::validateTabular(), I attempted to the following in my controller as well (on post):


CActiveForm::validateTabular($mForm, array('header'));

If the first for element is set, then all of it is set and valid (and copied over). I don’t think CActiveForm::validateTabular is what I want for this :)

Any ideas?

Thanks!