I used this extension in two places on the same form, I will give you the code for the simpler implementation. The simple implementation was for a keyword field and the more complex one was for an author field. The authors had to be formatted in a very specific way because the result was a scientific publication. I can give you the code for the more complex version if you think it will help, but I doubt you’ll need it.
First, calling the widget in the form view file.
<?php $this->widget('application.extensions.appendo.JAppendo',array(
'id' => 'keywordsTool',
'form' => $form,
'model' => $model,
'maxRows'=>Yii::app()->params->maxWords,
'labelAdd'=>'Add Keyword',
'allowDelete'=>false,
'viewName' => 'application.views.eventAbstract._keywords_form',
)); ?>
This is _keywords_form.php
<table class="appendo-gii" id="<?php echo $id ?>">
<caption>
<h4>Keywords</h4>
<em>Please enter the keywords in the order that you would like them to appear. To add a keyword, click 'Add Keyword'
</em>
<div class="clear">Please choose a minimum of <?php echo Yii::app()->params->minWords ?> and a maximum of <?php echo Yii::app()->params->maxWords ?> keywords.</div>
<div class="clear">To remove a keyword delete the text in the box, if you put a comma in your keyphrase it will count as more than one keyword.</div>
</caption>
<tbody>
<tr>
<th>Keyword or Phrase</th>
</tr>
<?php if ($model->keywords == null): ?>
<tr>
<td><?php echo CHtml::textField('keywordArray[]','',array('size'=>40)); ?></td>
</tr>
<?php else: ?>
<?php
$delim = ','; // this is the delimeter we will use
$keywordArray = explode($delim,$model->keywords);
$keys = count($keywordArray );
?>
<?php for($i = 0; $i < $keys; ++$i): ?>
<tr>
<td><?php echo CHtml::textField('keywordArray[]',$keywordArray[$i],array('size'=>30)); ?></td>
<?php endfor; ?>
<?php endif; ?>
</tbody>
</table>
This is the update method in the controller
public function actionUpdate($id)
{
$model=$this->loadModel($id);
$status = null;
$this->layout = '//layouts/column1';
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
$states = array('accepted','scheduled','rejected','on-hold');
foreach($states as $state){
if($model->swGetStatus()->getLabel() == $state)
{
$status = 1;
break;
}
}
if(MyUtils::abstractOwner($model->id) && $status == 1)
{
Yii::app()->user->setFlash('restricted','You are no longer allowed to edit this abstract.');
$this->redirect(array('view','id'=>$model->id));
}
if(isset($_POST['EventAbstract']))
{
$model->attributes=$_POST['EventAbstract'];
$keywordArray = (!isset($_POST['keywordArray']) ? array() : $_POST['keywordArray']);
$this->setKeywords($model,'keywords',$keywordArray);
$this->setAuthorField($model,$_POST['authorName'],$_POST['authorAffiliation'],$_POST['authorAffiliationLocation'],$_POST['authorRole'],$_POST['authorStudent']); if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('update',array(
'model'=>$model,
));
}
These two methods handle the array
public function setKeywords($model,$field,$keys)
{
foreach(array_keys($keys) as $key)
{
if (strlen($keys[$key]) < 1)
{
unset($keys[$key]); // take it out of the array
}
}
$model->$field = (empty($keys) ? null : implode(',',$keys));
}
public function getKeywords($delimited)
{
$keys = explode(',',$delimited);
return $keys;
}
Let me know if you have any questions.
doodle