I’ve followed the usage method of MultiFormModel extension from the extension page. But after i submit the form, nothing happens. The form is not getting validated or saved.
Here is actionCreate:
public function actionCreate()
{
Yii::import('ext.multimodelform.MultiModelForm');
$model=new Product;
$spec=new ProductSpec;
$validatedMembers = array();
$category=Category::model()->findAll(array('order'=>'category_name'));
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Product']))
{
$model->attributes=$_POST['Product'];
if(MultiModelForm::validate($spec,$validatedMembers,$deleteItems) && $model->save())
{
$masterValues = array ('product_id'=>$model->id);
if (MultiModelForm::save($spec,$validatedMembers,$deleteMembers,$masterValues))
$this->redirect(array('view','id'=>$model->id));
}
}
$this->render('create',array(
'model'=>$model,
'category'=>$category,
'spec'=>$spec,
'validatedMembers' => $validatedMembers,
));
}
In create.php,
<?php echo $this->renderPartial('_form', array('model'=>$model,'category'=>$category, 'spec'=>$spec,
'validatedMembers' => $validatedMembers,)); ?>
In _form.php
<?php
// see http://www.yiiframework.com/doc/guide/1.1/en/form.table
// Note: Can be a route to a config file too,
// or create a method 'getMultiModelForm()' in the member model
$memberFormConfig = array(
'elements'=>array(
'field1'=>array(
'type'=>'text',
'maxlength'=>40,
),
'field2'=>array(
'type'=>'text',
'maxlength'=>40,
),
));
$this->widget('ext.multimodelform.MultiModelForm',array(
'id' => 'id_member', //the unique widget id
'formConfig' => $memberFormConfig, //the form configuration array
'model' => $spec, //instance of the form model
//if submitted not empty from the controller,
//the form will be rendered with validation errors
'validatedItems' => $validatedMembers,
//array of member instances loaded from db
'data' => $spec->findAll('product_id=:prodId', array(':prodId'=>$model->id)),
));
?>
Product Model:
<?php
/**
* This is the model class for table "product".
*
* The followings are the available columns in table 'product':
* @property integer $id
* @property string $name
* @property string $menu_entry
* @property integer $shown_in_menu
* @property string $content
* @property integer $category_id
*
* The followings are the available model relations:
* @property Category $category
* @property ProductSpec[] $productSpecs
*/
class Product extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return Product the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'product';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('name, menu_entry, shown_in_menu, content, category_id', 'required'),
array('shown_in_menu, category_id', 'numerical', 'integerOnly'=>true),
array('name, menu_entry', 'length', 'max'=>200),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, name, menu_entry, shown_in_menu, content, category_id', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'category' => array(self::BELONGS_TO, 'Category', 'category_id'),
'productSpecs' => array(self::HAS_MANY, 'ProductSpec', 'product_id'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'name' => 'Name',
'menu_entry' => 'Menu Entry',
'shown_in_menu' => 'Shown In Menu',
'content' => 'Content',
'category_id' => 'Category',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('name',$this->name,true);
$criteria->compare('menu_entry',$this->menu_entry,true);
$criteria->compare('shown_in_menu',$this->shown_in_menu);
$criteria->compare('content',$this->content,true);
$criteria->compare('category_id',$this->category_id);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
ProductSpec Model
<?php
/**
* This is the model class for table "product_spec".
*
* The followings are the available columns in table 'product_spec':
* @property integer $id
* @property integer $product_id
* @property string $field1
* @property string $field2
*
* The followings are the available model relations:
* @property Product $product
*/
class ProductSpec extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return ProductSpec the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'product_spec';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('product_id, field1, field2', 'required'),
array('product_id', 'numerical', 'integerOnly'=>true),
array('field1, field2', 'length', 'max'=>1000),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, product_id, field1, field2', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'product' => array(self::BELONGS_TO, 'Product', 'product_id'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'product_id' => 'Product',
'field1' => 'Field1',
'field2' => 'Field2',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('product_id',$this->product_id);
$criteria->compare('field1',$this->field1,true);
$criteria->compare('field2',$this->field2,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}