2013-01-05 2 views
0

현재 yii에서 작업 중이며 user registraion, user login & change password rule으로 구성된 사용자 모듈을 설계했습니다. 이 3 가지 프로세스에 대한 은 하나만 설계했습니다. modeluser model입니다. 이 모델에 나는 규칙을 정의 :
하나의 모델을 두 가지 형태로 사용하는 방법은 무엇입니까?

array('email, password, bus_name, bus_type', 'required'), 

이 규칙은 actionRegister 유효합니다. 하지만 지금은

array('password, conf_password', 'required'), 

가 어떻게이 작업에 대한 규칙을 정의 할 수 있습니다, actionChangePassword의 새로운 required rule 정의하려면?

답변

3

Rules can be associated with scenarios. 특정 규칙은 모델의 현재 scenario 속성에 명시된 경우에만 사용됩니다.

샘플 모델 코드 :

class User extends CActiveRecord { 

    const SCENARIO_CHANGE_PASSWORD = 'change-password'; 

    public function rules() { 
    return array(
     array('password, conf_password', 'required', 'on' => self::SCENARIO_CHANGE_PASSWORD), 
    ); 
    } 

} 

샘플 컨트롤러 코드 :

public function actionChangePassword() { 
    $model = $this->loadModel(); // load the current user model somehow 
    $model->scenario = User::SCENARIO_CHANGE_PASSWORD; // set matching scenario 

    if(Yii::app()->request->isPostRequest && isset($_POST['User'])) { 
    $model->attributes = $_POST['User']; 
    if($model->save()) { 
     // success message, redirect and terminate request 
    } 
    // otherwise, fallthrough to displaying the form with errors intact 
    } 

    $this->render('change-password', array(
    'model' => $model, 
)); 
} 
+0

내가 만든 덕분에 @DCoder –

관련 문제