Situation
I used the wiki article on the Yii website, Collecting Table Entries , to follow suit.
I do not believe that I need to check table input in the traditional sense for multiple models. I have only one model, but I am dynamically creating the number of fields in the form. Here is a bit more background.
I import CSV files, where their headers differ in order among different files. Before properly disassembling the files, the user needs to match which title will be displayed in which table / column.
I have one model, ImportParseForm extended from CFormModel . This really has only one rule:
public function rules() { return array( array('header', 'required'), ); }
Here is a snippet of my view:
<?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; ?>
Here is a snippet of my controller:
$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){
If all fields are valid, it passes as expected. The problem is that if one of the fields is invalid (or empty in this case), then all fields are marked as invalid.
In Yii 1.1.10, they added CActiveForm :: validateTabular () , but this seems like a few models. Not quite what I have here. But for the punches, I added the following to my controller (of course, removed another type of check):
CActiveForm::validateTabular($mForm, array('header'));
The form itself is valid only when filling out the first element. If the first element is full, it will set all other elements with the same value (and passes the check).
Question
Basically, can I use CActiveForm to validate the generated dynamic fields (similar to table input, but with only one model)?