I’m sorry. I said wrong. I mean getting attributes with using $_POST array, of course.
I code an individual CFormModel, as waterloomatt suggests:
class ManageCategoryForm extends CFormModel
{
public $categoryID; // integer
public $newName; // string
/**
* Declares the validation rules.
*/
public function rules()
{
return array(
array('categoryID, newName', 'required'),
array('newName', 'length', 'max'=>48, 'min'=>3),
);
}
}
Here is code of my action:
/**
* Updates a name of particular category model.
* ID of the model is gets from user's request
*/
public function actionUpdateCategory()
{
$model=TblTask::model()->thisUser()->findAll(); //thisUser - scope
$model2=TblCategory::model()->thisUser()->findAll();
$model3=ManageCategoryForm;
if(isset($_POST['ManageCategoryForm']))
{
$model3->attributes=$_POST['ManageCategoryForm'];
if($model3->save())
{
Yii::app()->user->setFlash('updateCategory','Изменения сохранены.');
$this->refresh();
}
}
$this->render('updateCategory',array(
'model'=>$model, /* all the user's tasks */
'model2'=>$model2, /* all the user's categories */
'model3'=>$model3, /* form for selecting categoryName to change */
));
}
and this is my view _formCategoryUpdate, which renders in updateCategory.php
<?php
/* @var $this TaskController */
/* @var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'tbl-category-form',
'enableAjaxValidation'=>false,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>
<?php if(Yii::app()->user->hasFlash('_formCategoryUpdate')): ?>
<div class="flash-success">
<?php echo Yii::app()->user->getFlash('_formCategoryUpdate'); ?>
</div>
<?php else: ?>
<p class="note">Поля, отмеченные <span class="required">*</span> являются обязательными.</p>
<div class="row">
Выберите категорию для изменения: <br />
<?php echo $form->labelEx($model3,'categoryID'); ?>
<?php echo $form->dropDownList($model3,'categoryID', CHtml::listData($model2, 'ID', 'Name')); ?>
<?php echo $form->error($model3,'categoryID'); ?>
</div>
<div class="row">
Введите новое имя категории: <br />
<?php echo $form->labelEx($model3,'newName'); ?>
<?php echo $form->textField($model3,'newName'); ?>
<?php echo $form->error($model3,'newName'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Создать категорию' : 'Сохранить изменения'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
<?php endif; ?>
I get PHP warning because of discrepancy of what gets in $form->labelEx, $form->dropDownList, $form->error.
In this view I want user to select Category he wants to change name of, and newName for this Category