I have a module named as usergroup I am creating a controller and the controller code is
<?php
class ModulesController extends Controller
{
public $userid='';
public $usernameuser='';
public $modules=array();
/**
* @var mixed tooltip for the permission menagement
*/
/*public static $_permissionControl = array(
//'write'=>'can invite other users.',
'admin'=>'can access all the modules',
);*/
/**
* @return array action filters
*/
public function filters()
{
return array(
'accessControl', // perform access control for CRUD operations
);
}
public function beforeAction()
{
$obj=new SideMenu;
$modulesarray=$obj->GetAllModules('0');
$this->modules=$modulesarray;
return true;
}
/**
* Specifies the access control rules.
* This method is used by the 'accessControl' filter.
* @return array access control rules
*/
public function accessRules()
{
return array( array('allow', // allow logged user to access the userlist page if the have admin rights on users or if the list is public
'actions'=>array('index'),
'users'=>array('@'),
),
array('allow', // allow guest to view users profiles according to the configuration
'actions'=>array('view'),
'users'=>array('?','@'),
),
array('allow', // allow a user tu open an update view just on their own accounts
'actions'=>array('AddModule'),
'users'=>array('?','@'),
),
);
}
public function actionAddModule()
{
$model=new UserGroupsModulesNew;
$allmodules=$this->getAllModules();
$mainmodules=CHtml::listData($allmodules,'id','display_name');
if(isset($_POST['ajax']) && $_POST['ajax']==='user-groups-modules-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
if(isset($_POST['UserGroupsModulesNew']))
{
if($model->validate())
{
if(isset($_POST['UserGroupsModulesNew']['group_module']) && $_POST['UserGroupsModulesNew']['group_module']!='' && $_POST['UserGroupsModulesNew']['group_module_old']=='')
{
//to update the tmp_images tablee
$modulegroupname=ucfirst($_POST['UserGroupsModulesNew']['group_module']." Management.");
$sqlupdate = "insert into usergroups_modules set display_name='".$modulegroupname."',status='1',delete_flag='0',controller='".$_POST['UserGroupsModulesNew']['group_module']."',parent_id=0";
Yii::app()->db->createCommand($sqlupdate)->execute();
//end of updating the images table
}
if(isset($_POST['UserGroupsModulesNew']['display_name']))
{
$group_moduleid=Yii::app()->db->getLastInsertID();
$modulename=ucfirst($_POST['UserGroupsModulesNew']['display_name']);
$sqlupdate = "insert into usergroups_modules set display_name='".$modulename."',status='1',delete_flag='0',controller='".$_POST['UserGroupsModulesNew']['group_module']."',parent_id='".$group_moduleid."'";
Yii::app()->db->createCommand($sqlupdate)->execute();
//end of updating the images table
}
$this->redirect(array('user/index'));
}
else
{
print_r($model->validate());
print_r($model->getErrors());print_r($_POST);
}
}
$this->render('add',array('model'=>$model,'mainmodules'=>$mainmodules));
}
protected function performAjaxValidation($model)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='user-groups-modules-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
public function getAllModules()
{
$criteria=new CDbCriteria;
$criteria->condition='status=:status and delete_flag=:delete_flag and parent_id=:parent_id';
$criteria->params=array(':status'=>'1',':delete_flag'=>'0',':parent_id'=>0);
$getallmodules=UserGroupsModulesNew::model()->findAll($criteria);
return $getallmodules;
}
}
and the model code is
<?php
/**
-
This is the model class for table "usergroups_modules".
-
The followings are the available columns in table ‘usergroups_modules’:
-
@property integer $id
-
@property string $display_name
-
@property integer $display_order
-
@property integer $parent_id
-
@property string $status
-
@property string $delete_flag
*/
class UserGroupsModulesNew extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @return UserGroupsModules the static model class
*/
public $group_module_old;
public $group_module;
public $submoduleurl;
public $submodule;
public $moduleurl;
public $module;
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return Yii::app()->db->tablePrefix.'usergroups_modules';
}
/**
* @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('display_name', 'required'),
);
}
/**
* @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(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'display_name' => 'Display Name',
'display_order' => 'Display Order',
'parent_id' => 'Parent',
'status' => 'Status',
'delete_flag' => 'Delete Flag'
);
}
/**
* 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('display_name',$this->display_name,true);
$criteria->compare('display_order',$this->display_order);
$criteria->compare('parent_id',$this->parent_id);
$criteria->compare('status',$this->status,true);
$criteria->compare('delete_flag',$this->delete_flag,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
and the view code is
<?php
$this->breadcrumbs=array(
Yii::t('userGroupsModule.general','Module Management'),
);
?>
<div id="userGroups-container">
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'user-groups-modules-form',
'enableAjaxValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
'validateOnType'=>false,
'validateOnChange'=>false
)
)); ?>
<div class="row">
<?php echo $form->labelEx($model,'group_module'); ?>
<?php echo $form->textField($model,'group_module'); ?>
<?php echo $form->error($model,'group_module'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'group_module_old'); ?>
<?php echo $form->dropDownList($model,'group_module_old',$mainmodules,array('prompt'=>'--select--')); ?>
<?php echo $form->error($model,'group_module_old'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'display_name'); ?>
<?php echo $form->textField($model,'display_name'); ?>
<?php echo $form->error($model,'display_name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'moduleurl'); ?>
<?php echo $form->textField($model,'moduleurl'); ?>
<?php echo $form->error($model,'moduleurl'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'submodule'); ?>
<?php echo $form->textField($model,'submodule'); ?>
<?php echo $form->error($model,'submodule'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'submoduleurl'); ?>
<?php echo $form->textField($model,'submoduleurl'); ?>
<?php echo $form->error($model,'submoduleurl'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Add Module'); ?>
</div>
<?php $this->endWidget(); ?>
</div>
</div>
I don’t no the problem but when i use $model->validate it always returns false and $model->getErrors() always give errors.I have made loads of forms using the same code they all worled but strange it doesn’t work .Please help me out yii guysssssssssssssssssssssssssssssss I am stuck