Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7615189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:35:07+00:00 2026-05-31T02:35:07+00:00

Situation I used the wiki article on Yii’s site, Collecting Tabular Input , to

  • 0

Situation

I used the wiki article on Yii’s site, Collecting Tabular Input, to follow as an example.

I don’t believe I need to validate tabular input in a traditional sense against multiple models. I only have one model, but I’m dynamically creating the number of fields in the form. Here’s a bit more background.

I’m importing CSV files where its headers vary in order among the different files. Before correctly parsing the files, the user needs to map which header would map to what table/column.

I have a single model, ImportParseForm extended from CFormModel. It really only has one rule:

public function rules()
{
    return array(
        array('header', 'required'),
    );
}

Here’s 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’s 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){
                // Process CSV
            }
        }

If all fields are valid, then it passes as expected. The problem is if one of the fields are invalid (or in this case, empty), then all fields are flagged as invalid.

In Yii 1.1.10, they added CActiveForm::validateTabular(), but it looks like it’s for multiple models. Not quite what I have here. But for kicks, I added the following to my controller (removed the other type of validation, of course):

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

The form itself is only valid if the first element is populated. If the first element is populated, it will set all the other elements with that same value (and passes validation).

Question

Basically, can I use CActiveForm to do validation against fields that are dynamically generated (similar to tabular input, but with only one model)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:35:08+00:00Added an answer on May 31, 2026 at 2:35 am

    After reading Collecting Tabular Input a bit closer, I am using “multiple” models. I misunderstood that multiple models would mean multiple different structured models and not just multiple of the same structured model in an array. For example, in the wiki there’s a piece that shows what items (array of models) to update: $items=$this->getItemsToUpdate();. My corrected assumption is that that particular method grabs multiple of the same structured model but with different primary keys… or different records. Understanding that, the rest of the article makes more sense 😉

    But here’s my model solution on how to create a CSV header mapping form.

    class ImportParseForm extends CFormModel{
    
        // Model really only has one attribute to check against, the header
        var $header;
        // New attributeLabels collected and stored on class instantiation
        protected $attributeLabels;
    
        // Modify construct so we can pass in custom attribute labels
        public function __construct($attributeLabels = '', $scenario = '')
        {
            if (! is_array($attributeLabels)){
                $this->attributeLabels = array($attributeLabels);
            }
            else{
                $this->attributeLabels = $attributeLabels;
            }
    
            parent::__construct($scenario);
        }
    
    
        public function rules()
        {
            return array(
                array('header', 'required'),
            );
        }
    
        public function attributeLabels()
        {
            // Default mapping
            $arr = array(
                'header' => 'Header Mapping',
            );
    
            // Merge mapping where custom labels overwrite default
            return array_merge($arr, $this->attributeLabels);
        }
    }
    

    Here is a snippet in my controller on what my equivalent to $items=$this->getItemsToUpdate(); (again, the goal is to collect an array of models) would look like

            // Get the first row of CSV, assume it's the headers
            $tmpCsvRow = explode("\n", $mTmp->data);
            $headers = explode(',', $tmpCsvRow[0]);
    
            foreach ($headers as $header){
                if (! empty($header)){ // Blank headers are lame, skip them
                    // Add a new model for each CSV header found into $mForm array
                    // You can also add in a custom attributeLabel, $header is an actual header name like 'First Name',
                    // so the new label for the header attribute in ImportParseForm would be 'First Name header' and
                    // it will show up properly in your CActiveForm view
                    $mForm[] = new ImportParseForm(array('header' => $header.' header'));
                }
            }
    

    Push $mForm to your view. Now in your view, iterate through $mForm for your form like so (similar to the wiki article, but I’m using a CActiveForm widget here):

    <?php foreach($mForm as $m => $mItem): ?>
    <div class="row">
        <?php echo $fParse->labelEx($mItem,"[$m]header"); ?> maps to
        <?php echo $fParse->textField($mItem, "[$m]header"); ?>
        <?php echo $fParse->error($mItem, "[$m]header"); ?>
    </div>
    <?php endforeach; ?>
    

    Validation works as expected.

    If you want to use AJAX validation, use CActiveForm::validateTabular() in your controller (instead of the normal validate()).

    Hope this helps other Yii beginners! 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In situation, when I need a webservice client, which will be used by some
I have a situation where I need the Checker enum below used in multiple
I've used databases in asp.net but I now have a situation where I need
I was talking with a co-worker yesterday regarding a situation where he used SSIS
I used to have a situation where I hit the database a every time
Hypothetical situation: let's say I have a 3rd party .net assembly being used in
Does anyone know a situation where a PostgreSQL HASH should be used instead of
This is in regards to a situation where Session is used to store some
Situation: I have a simple XML document that contains image information. I need to
I'm in a bit of a situation right now. I need software that does

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.