GSTAR
(Omzy83)
December 20, 2010, 9:53am
1
<?php foreach($models as $model) { ?>
<?php echo CHtml::activeLabel($model, 'title'); ?>
<?php echo CHtml::activeTextField($model, 'title', array('size'=>45)); ?>
<?php } ?>
This outputs a label and a textfield for several records on the same page. The problem here is that the label ‘FOR’ and input ‘ID’ attributes are exactly the same.
How can I generate unique IDs (and associated label FOR attributes)?
By the way I noticed CHTML has a property ‘count’ - integer - the counter for generating automatic input field names. Is this what I need and if so how do I use it?
zaccaria
(Matteo Falsitta)
December 20, 2010, 10:14am
2
No, you should collect tabular input.
There is no meaninigs in create 2 textbox with the same name, as the you will only get the value of the last one.
Tabular input cames for solve this kind of problems.
GSTAR
(Omzy83)
December 20, 2010, 10:18am
3
Thanks for that boss, I did not know about that trick
zaccaria
(Matteo Falsitta)
December 20, 2010, 10:22am
4
Yeah!! If you want there is an extension for manage this tabular input.
Is a bit complicated, but I guess it worth.
I’d be peased if you will give a try and give some feedback!
GSTAR
(Omzy83)
December 20, 2010, 10:37am
5
I will defintely give that a try when I get some free time.
I’m currently having a slight problem with this - when I submit the form only the first model is being validated:
<?php foreach($models as $i=>$model) { ?>
<?php echo CHtml::activeLabelEx($model, "[$i]title"); ?>
<?php echo CHtml::activeTextField($model, "[$i]title", array('size'=>45)); ?>
<?php } ?>
public function actionUpdate()
{
$models=Content::model()->findAll('user_id=:user_id', array(':user_id'=>Yii::app()->user->id));
if(isset($_POST['Content']))
{
$valid=true;
foreach($models as $i=>$model)
{
if(isset($_POST['Content'][$i]))
{
$model->attributes=$_POST['Content'][$i];
}
$valid=$valid && $model->validate();
}
if($valid)
{
// ...do something here
}
}
$this->render('update', array(
'models'=>$models,
));
}
zaccaria
(Matteo Falsitta)
December 20, 2010, 10:55am
6
I really advice to take a look to the extension, you are rewriting the same code (it will be more time saving using alread written code).
For your problem you should use:
$valid=$model->validate() && $valid;
Php is lazy, if $valid is false is not evaluating the second part of the &&, so you miss the validation.
GSTAR
(Omzy83)
December 20, 2010, 11:01am
7
Thanks again boss! I think that article needs to be amended to address this “bug”
zaccaria
(Matteo Falsitta)
December 20, 2010, 11:36am
8
I have never noticed before, I will leave a comment.
GSTAR
(Omzy83)
December 20, 2010, 12:00pm
9
Last question - how do I do this tabular input for a Radio button? For example:
<?php echo CHtml::activeLabel($model, "[$i]default_item"); ?>
<?php echo CHtml::activeRadioButton($model, "[$i]default_item"); ?>
Each model has a label and a radio button; only one radio button can be selected from all the models.
EDIT: I resolved this as follows:
<?php echo CHtml::activeLabel($model, "[$i]default_item"); ?>
<?php echo CHtml::activeRadioButton($model, "default_item", array(
'value'=>$model->id,
'id'=>CHtml::activeId($model, "[$i]default_item"),
'uncheckValue'=>null,
)); ?>