<commit_before>Collecting Tabular Input
========================

Sometimes we want to collect user input in a batch mode. That is, the user
can enter the information for multiple model instances and submit them all
at once. We call this *tabular input* because the input fields are
often presented in an HTML table.

To work with tabular input, we first need to create or populate an array
of model instances, depending on whether we are inserting or updating the
data. We then retrieve the user input data from the `$_POST` variable and
assign it to each model. A slight difference from single model input is
that we retrieve the input data using `$_POST['ModelClass'][$i]` instead of
`$_POST['ModelClass']`.

~~~
[php]
public function actionBatchUpdate()
{
	// retrieve items to be updated in a batch mode
	// assuming each item is of model class 'Item'
	$items=$this->getItemsToUpdate();
	if(isset($_POST['Item']))
	{
		$valid=true;
		foreach($items as $i=>$item)
		{
			if(isset($_POST['Item'][$i]))
				$item->attributes=$_POST['Item'][$i];
			$valid=$valid && $item->validate();
		}
		if($valid)  // all items are valid
			// ...do something here
	}
	// displays the view to collect tabular input
	$this->render('batchUpdate',array('items'=>$items));
}
~~~

Having the action ready, we need to work on the `batchUpdate` view to
display the input fields in an HTML table.

~~~
[php]
<div class="form">
<?php echo CHtml::beginForm(); ?>
<table>
<tr><th>Name</th><th>Price</th><th>Count</th><th>Description</th></tr>
<?php foreach($items as $i=>$item): ?>
<tr>
<td><?php echo CHtml::activeTextField($item,"[$i]name"); ?></td>
<td><?php echo CHtml::activeTextField($item,"[$i]price"); ?></td>
<td><?php echo CHtml::activeTextField($item,"[$i]count"); ?></td>
<td><?php echo CHtml::activeTextArea($item,"[$i]description"); ?></td>
</tr>
<?php endforeach; ?>
</table>

<?php echo CHtml::submitButton('Save'); ?>
<?php echo CHtml::endForm(); ?>
</div><!-- form -->
~~~

Note in the above that we use `"[$i]name"` instead of `"name"` as the
second parameter when calling [CHtml::activeTextField].

If there is anything validation error, the corresponding input fields will
be highlighted automatically, just like the single model input we described
earlier on.

<div class="revision">$Id$</div><commit_msg>(Fixes issue 1270) a word is missing on "Collecting Tabular Input" page<commit_after>Collecting Tabular Input
========================

Sometimes we want to collect user input in a batch mode. That is, the user
can enter the information for multiple model instances and submit them all
at once. We call this *tabular input* because the input fields are
often presented in an HTML table.

To work with tabular input, we first need to create or populate an array
of model instances, depending on whether we are inserting or updating the
data. We then retrieve the user input data from the `$_POST` variable and
assign it to each model. A slight difference from single model input is
that we retrieve the input data using `$_POST['ModelClass'][$i]` instead of
`$_POST['ModelClass']`.

~~~
[php]
public function actionBatchUpdate()
{
	// retrieve items to be updated in a batch mode
	// assuming each item is of model class 'Item'
	$items=$this->getItemsToUpdate();
	if(isset($_POST['Item']))
	{
		$valid=true;
		foreach($items as $i=>$item)
		{
			if(isset($_POST['Item'][$i]))
				$item->attributes=$_POST['Item'][$i];
			$valid=$valid && $item->validate();
		}
		if($valid)  // all items are valid
			// ...do something here
	}
	// displays the view to collect tabular input
	$this->render('batchUpdate',array('items'=>$items));
}
~~~

Having the action ready, we need to work on the `batchUpdate` view to
display the input fields in an HTML table.

~~~
[php]
<div class="form">
<?php echo CHtml::beginForm(); ?>
<table>
<tr><th>Name</th><th>Price</th><th>Count</th><th>Description</th></tr>
<?php foreach($items as $i=>$item): ?>
<tr>
<td><?php echo CHtml::activeTextField($item,"[$i]name"); ?></td>
<td><?php echo CHtml::activeTextField($item,"[$i]price"); ?></td>
<td><?php echo CHtml::activeTextField($item,"[$i]count"); ?></td>
<td><?php echo CHtml::activeTextArea($item,"[$i]description"); ?></td>
</tr>
<?php endforeach; ?>
</table>

<?php echo CHtml::submitButton('Save'); ?>
<?php echo CHtml::endForm(); ?>
</div><!-- form -->
~~~

Note in the above that we use `"[$i]name"` instead of `"name"` as the
second parameter when calling [CHtml::activeTextField].

If there are validation errors, the corresponding input fields will
be highlighted automatically, just like the single model input we described
earlier on.

<div class="revision">$Id$</div>
{'fraction_non_alphanumeric': 0.16906243295430165, 'fraction_numerical': 0.0008581849388543231, 'mean_word_length': 5.207723035952064, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1748114', 'n_tokens_mistral': 1426, 'n_tokens_neox': 1390, 'n_words': 537}