I have the attribute pass that is not null in the database,then in model o want this pass required just on create scenario ,for that i have these rules in the model:
public function rules() {
return array(
array('nome, username, email,country', 'required'),
array('receivenews', 'numerical', 'integerOnly' => true),
array('username,email', 'unique'),
array('nome, email', 'length', 'max' => 145),
array('username', 'length', 'max' => 45),
array('pass', 'length', 'max' => 95),
array('pass', 'required', 'on' => 'create'),
array('iduser, nome, username,country,receivenews, lastLogin, email, createDate', 'safe', 'on' => 'search'),
);
}
my controller update action and loadmodel:
public function loadModel($id) {
$model = User::model()->findByPk($id);
if ($model === null || ($id != Yii::app()->user->id && !Yii::app()->user->checkAccess('Admin', Yii::app()->user->id)))
throw new CHttpException(404, 'The requested page does not exist.');
return $model;
}
public function actionUpdate($id) {
$model = $this->loadModel($id);
if (isset($_POST['User'])) {
$model->attributes = $_POST['User'];
if ($model->validate()) {
if ($model->isOriginalPass()) {
$model->saveAttributes(array('nome', 'email', 'country', 'username', 'receivenews'));
} else {
$model->save();
}
Yii::app()->user->setFlash('success', Yii::t('app', 'Data saved'));
$this->redirect(array('view', 'id' => $model->iduser));
}
}
$this->render('update', array(
'mostraPass' => false,
'model' => $model
));
}
Now on model->save i check the model->attributes and pass value is there but it wont save in database,why?