hi Experts
im trying to develop a master detail for in yii
i have two tables
category (categoryID,CategoryName,CategoryType)
SubCategory (SubCategoryID,SubCategoryName,CategoryID)
category and SubCategoory Have 1 to many relationship
im unable to show error message if the sub category name is empty but i have set the required rule in my subcategory model
please help me
here is the code for my view file
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'catsubcat-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>
<?php echo $form->errorSummary($catModel); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<div class="row">
<?php echo $form->labelEx($catModel,'CategoryName'); ?>
<?php echo $form->textField($catModel,'CategoryName',array('size'=>40,'maxlength'=>100)); ?>
<?php echo $form->error($catModel,'CategoryName'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($catModel,'CatType'); ?>
<?php echo $form->dropDownList($catModel,'CatType',Category::model()->GetCategoryTypes(),array('prompt'=>'Select One')); ?>
<?php echo $form->error($catModel,'CatType'); ?>
</div>
<table id="subcategoryTable">
<tr><th>Name</th></tr>
<?php $this->renderPartial('/category/_form', array('model'=>new Subcategory())); ?>
</table>
<div class="row buttons">
<?php echo CHtml::submitButton('Save'); ?>
<input type="button" id="addNewField" value="ADD"/>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
<?php
Yii::app()->clientScript->registerScript('AddNewField',
'$("#addNewField").click(function(e){
var currentIndex= $("#subcategoryTable tr:last").index();
if(currentIndex>0)
currentIndex=currentIndex++;
$.ajax({
url: "/top3dmodels/index.php/category/create",
type: "POST",
data: { i: currentIndex},
datatype: "html",
success: function(res) {
$("#subcategoryTable").append(res);
}
})
});',
CClientScript::POS_READY);
?>
code for my partial render form
<?php if(isset($_POST['i']))
$rowIndex=$_POST['i'];
else
$rowIndex=0;
?>
<tr>
<td>
<div class="row">
<?php echo CHtml::activeTextField($model,"[$rowIndex]SubCatName",array('size'=>40,'maxlength'=>100)); ?>
</div>
</td>
</tr>
and my controller action method
public function actionCreate(){
if (Yii::app()->request->isAjaxRequest) {
$model = new Subcategory();
$html = $this->renderPartial('/category/_form', array('model' => $model), true);
echo $html;
Yii::app()->end();
}
$catModel= new Category();
$subCatsToSave=array();
$valid=true;
if(isset($_POST['Category']))
{
$catModel->attributes = $_POST['Category'];
$valid=$catModel->validate();
if($valid)
{
if(isset($_POST['Subcategory']))
{
foreach ($_POST['Subcategory'] as $name)
{
$subModel=new Subcategory();
$subModel->SubCatName=$name["SubCatName"];
$valid=$subModel->validate() && $valid;
if($valid)
{
$subCatsToSave[]=$subModel;
}
}
if($valid)
{
//$this->dump($subModel);
//save category model here
$catModel->save();
//save subcategory model here
foreach ($subCatsToSave as $sub)
{
$sub->CategoryID=$catModel->CategoryID;
$sub->save();
}
}
}
}
}
$this->render('create',array('catModel'=>$catModel));
}
my categoory model rules function
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('CategoryName,CatType','required'),
array('CategoryName, CatType', 'length', 'max'=>100),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('CategoryID, CategoryName, CatType', 'safe', 'on'=>'search'),
);
}
my sub category model rules function
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('SubCatName', 'required'),
array('SubCatName', 'length', 'max'=>100),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('SubCatID, SubCatName, CategoryID', 'safe', 'on'=>'search'),
);
}