Commits

christiansalazar committed 2b37411

CrugeStoredUser validador duplicados bajo escenario update

Comments (0)

Files changed (1)

models/data/CrugeStoredUser.php

 		return $validateResult;
 	}
 	public function save(){
-		if($this->hasErrors())	
+		Yii::log(__METHOD__,"info");
+		if($this->hasErrors()){
+			Yii::log(__METHOD__." return false, has errors.","info");
 			return false;
+		}
 		
 		// importante aqui:
 		// primero debe guardar el usuario (this) y luego los campos
 		$ok = parent::save();
 		$this->saveFields();
 		
+		Yii::log(__METHOD__." returns: [".$ok."]","info");
 		return $ok;
 	}
 	public function saveFields(){
 			{
 				// buscar el objeto ICrugeFieldValue, darle valores y guardarlo
 				$crugeFieldValueInst = Yii::app()->user->um->loadICrugeFieldValue($this,$f);
+				$boolOk = false;
 				if($crugeFieldValueInst != null){
 					$crugeFieldValueInst->value = $f->getFieldValue();
-					$crugeFieldValueInst->save();
+					$boolOk = $crugeFieldValueInst->save();
 				}
+				Yii::log("\n".__METHOD__." \nfieldname='".$f->fieldname."'\nfieldvalue='".$f->getFieldValue()
+					."'\n boolOk=[".$boolOk."]\ncrugeFieldValueInst=[".($crugeFieldValueInst==null ? 'null' : 'not null')."]\n\n","info");
 			}
 	}
 	
 			array('authkey', 'length', 'max'=>100),
 			array('email', 'email'),
 			array('email', 'length', 'max'=>100),
-			array('username,email', 'unique'),
+			
+			array('username,email', 'validate_unique'),
 			
 			array('deleteConfirmation','required','on'=>'delete'),
 			array('deleteConfirmation','compare','compareValue'=>'1'
 		);
 	}
 	
+	
+	public function validate_unique($att, $params){
+		$model = self::model()->findByAttributes(array($att=>$this[$att]));
+		if($model != null){
+			$duptext = CrugeTranslator::t("El '".$att."' indicado ya esta en uso");
+			if($this->scenario == 'insert'){
+				$this->addError($att,$duptext);
+				return;
+			}
+			if($this->scenario == 'update'){
+				if($this->iduser != $model->iduser)
+					$this->addError($att,$duptext);
+				return;
+			}
+		}
+	}	
 	/**
 	 * @return array relational rules.
 	 */