资源说明:Yii Community: Composite form extension
Yii Composite Form Extension ========================== The extension that can greatly simplify processing of complex forms with multiple relations. [Weavora's](http://weavora.com) Git Repo - [https://github.com/weavora/wform](https://github.com/weavora/wform) **Features**: * Easy composite form processing * Fast configuration * Support of all standard relations: has_one, belongs_to, has_many and many_many Configuration ----- 1) Download and unpack the source into the protected/extensions/ folder. 2) Below you can see the config settings for import: ```php array( ... 'ext.wform.*', ), ... ); ``` 3) The extension requires changing CActiveRecord::onUnsafeAttribute. Here are a few options for that: a) Extend all your models/forms from WActiveRecord instead of CActiveRecord b) If you have already modified the class for active record, extend it from WActiveRecord or add the onUnsafeAttribute method: ```php $name, 'value' => $value)); $this->raiseEvent('onUnsafeAttribute', $event); return parent::onUnsafeAttribute($name, $value); } } ``` Usage ----- 1) Modify the model: define relations and attach behavior. You can also create a separate class for the form extended from your model. ```php array(self::HAS_ONE, 'HasOneModel', 'my_model_fk_into_related_model'), 'belongsToRelation' => array(self::BELONGS_TO, 'BelongsToModel', 'related_model_fk_into_my_model'), 'hasManyRelation' => array(self::HAS_MANY, 'HasManyModel', 'my_model_fk_into_related_model'), 'manyManyRelation' => array(self::MANY_MANY, 'ManyManyModel', 'linker(my_model_id,related_model_id)'), ); } ... public function behaviors() { return array( // attach wform behavior 'wform' => array( 'class' => 'ext.wform.WFormBehavior', // define relations which would be processed 'relations' => array('hasOneRelation', 'belongsToRelation', 'hasManyRelation', 'manyManyRelation'), ), // or you could allow to skip some relation saving if it was submitted empty 'wform' => array( 'class' => 'ext.wform.WFormBehavior', 'relations' => array( 'hasOneRelation' => array( 'required' => true, // declare that a relation item should be valid (default for HAS_ONE: false) 'cascadeDelete' => true, // declare if a relation item would be deleted during parent model deletion (default for HAS_ONE: true) ), 'belongsToRelation' => array( 'required' => true, // declare all relation items to be valid (default for BELONGS_TO: false) ), 'hasManyRelation' => array( 'required' => true, // declare all relation items to be valid (default for HAS_MANY: false) 'unsetInvalid' => true, // will unset invalid relation items during save or validate actions (default for HAS_MANY: false) 'cascadeDelete' => true, // declare if relation items would be deleted during parent model deletion (default for HAS_MANY: true) ), 'manyManyRelation' => array( 'required' => true, // declare all relation items to be valid (default for MANY_MANY: false) 'unsetInvalid' => true, // will unset invalid relation items during save or validate actions (default for MANY_MANY: false) 'cascadeDelete' => true, // declare if db rows with a relation item link to model would be deleted during parent model deletion (default for MANY_MANY: true) ), ), ), ); } ... } ``` 2) Create an action to process the form. ```php with('hasManyRelation','manyManyRelation')->findByPk($id) : new MyModel(); if(Yii::app()->request->isPostRequest) { $myModel->attributes = Yii::app()->request->getPost('MyModel'); if ($myModel->save()) { $this->redirect('some/page'); } } $this->render('edit', array( 'model' => $myModel )); } // delete the model with relation to a single line of code :) public function actionDelete($id) { $myModel = MyModel::model()->findByPk($id); if(!empty($myModel)) { $myModel->delete(); } $this->redirect('some/page'); } } ``` 3) Include js/jquery.multiplyforms.js jquery plugin into your layout 4) Define the form using WForm instead of CActiveForm ```php // protected/views/my/edit.phpisNewRecord ? "Create" : "Update " . $model->name);?>
beginWidget('WForm'); ?>labelEx($model, 'name'); ?> textField($model, 'name'); ?> error($model, 'name'); ?>labelEx($model, 'hasOneRelation.name'); ?> textField($model, 'hasOneRelation.name'); ?> error($model, 'hasOneRelation.name'); ?>labelEx($model, 'belongsToRelation.name'); ?> textField($model, 'belongsToRelation.name'); ?> error($model, 'belongsToRelation.name'); ?>hasManyRelation): ?> hasManyRelation as $index => $item): ?>isNewRecord): ?> hiddenField($model, "hasManyRelation.$index.id"); ?> labelEx($model, "hasManyRelation.$index.text"); ?> textField($model, "hasManyRelation.$index.text"); ?> error($model, "hasManyRelation.$index.text"); ?> DeletelabelEx($model, "hasManyRelation..text"); ?> textField($model, "hasManyRelation..text"); ?> error($model, "hasManyRelation..text"); ?> DeleteAdd moremanyManyRelation): ?> manyManyRelation as $index => $item): ?>endWidget(); ?> ``` Real Examples ----- [product form example](https://github.com/weavora/wform/wiki/Example:-Product-form)isNewRecord): ?> hiddenField($model, "manyManyRelation.$index.id"); ?> labelEx($model, "manyManyRelation.$index.note"); ?> textField($model, "manyManyRelation.$index.note"); ?> error($model, "manyManyRelation.$index.note"); ?> DeletelabelEx($model, "manyManyRelation..note"); ?> textField($model, "manyManyRelation..note"); ?> error($model, "manyManyRelation..note"); ?> DeleteAdd more
本源码包内暂不包含可直接显示的源代码文件,请下载源码包。