[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!